Skip to content
Advertisement

Cluster geojson Layer doesn’t work with marker filtering

I’m doing a Leaflet map with some GeoJSON data.

I try to add the cluster function to my JS file. As I added some filter and styling features according to properties, I’m unable to find the right way to code the cluster fonction.

Here is the GeoJSON layer and the filter verificator:

JavaScript

with the syling function:

JavaScript

I add then the popup:

JavaScript

I try to add this piece of code to display the cluster but I don’t know where to place it in my code in order to work (https://github.com/Leaflet/Leaflet.markercluster):

JavaScript

My whole code is available here: https://github.com/jandre3/pince-crochet

Advertisement

Answer

Once you have populated your Leaflet GeoJSON Layer Group (typically with geojsonLayer.addData(geoJsonObject), then instead of adding that group to your map, simply add it into your MarkerClusterGroup:

JavaScript

If later on you want to swap the content, you can clear it from both groups and repeat:

JavaScript

Demo: https://jsfiddle.net/hd68ea7k/

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement