Clustering GeoJSON - best practice?

1,668 views
Skip to first unread message

Michal Zimmermann

unread,
Nov 13, 2015, 3:35:46 PM11/13/15
to Leaflet
Hi,
I've got thousands of points saved in geojson file. I'd like to cluster them with L.markerClusterGroup and yet have the option to toggle them separately in the layers control. You can see what I've tried so far in the code below.

json = L.geoJson(data, {
        onEachFeature
: function(feature, layer) {
            layer
.bindPopup(L.Util.template("<h1>{title}</h1><p>{description}</p>", {
                title
: feature.properties.title,
                description
: feature.properties.description
           
}));

            category
= feature.properties.location_type;

           
if (typeof categories[category] == "undefined") {
                categories
[category] = [];
           
}

            categories
[category].push(layer);
       
},
        pointToLayer
: function(feature, position) {
           
return L.marker(position, {icon: L.AwesomeMarkers.icon(styles[feature.properties.location_type])});
       
}
   
});

    cluster
= L.markerClusterGroup({
        chunkedLoading
: true,
        chunkInterval
: 500
   
});
    cluster
.addTo(map);

    map
.on("overlayadd", function(e) {
        map
.removeLayer(e.layer);
        cluster
.addLayer(e.layer);
   
})

    map
.on("overlayremove", function(e) {
        cluster
.removeLayer(e.layer);
   
})

   
for (var category in categories) {
        overlays
[styles[category].type] = L.layerGroup(categories[category]);
   
}

The performance is really poor due to what happens when overlay is added to the map. I haven't found a way to supress default behavior of map.overlayadd event. Is there any way to add layer to the cluster without letting it into the map itself?

Anika Halota

unread,
Nov 13, 2015, 4:15:19 PM11/13/15
to Leaflet
I can't see in the code you posted where you are adding the layers to the map. Can you post more of it or better yet create a https://jsfiddle.net/ with a subset of your data ?

Michal Zimmermann

unread,
Nov 14, 2015, 1:38:19 AM11/14/15
to leafl...@googlegroups.com

--

---
You received this message because you are subscribed to a topic in the Google Groups "Leaflet" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/leaflet-js/5gQGCbU4meA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to leaflet-js+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Michal Zimmermann (zimmi) 
WWW: http://www.zimmi.cz

ghybs

unread,
Nov 14, 2015, 10:47:10 AM11/14/15
to Leaflet
Hi,

I think you would be interested in that post:
https://gis.stackexchange.com/questions/169011/leaflet-how-to-create-toggleable-overlays-from-a-single-geojson-featurecollecti/169038#169038

I also just released 2 plugins that I think would fit your need:
- Leaflet.FeatureGroup.SubGroup Creates a Feature Group that adds its child layers into a parent group when added to a map (e.g. through L.Control.Layers). Typical usage is to dynamically add/remove groups of markers from Marker Cluster.
- Leaflet.MarkerCluster.LayerSupport Brings compatibility with L.Control.Layers and other Leaflet plugins. I.e. everything that uses direct calls to map.addLayer and map.removeLayer.

Hope this helps.

Michal Zimmermann

unread,
Nov 14, 2015, 10:49:20 AM11/14/15
to leafl...@googlegroups.com
Hi,
I've just implemented subgroup today to my code and it's great! Thanks a lot.
Reply all
Reply to author
Forward
0 new messages