[GIS] Leaflet markercluster – cluster/uncluster toggle

leaflet

Is it possible to toggle between clustered and unclustered with a leaflet MarkerClusterGroup?

I know I can disable clustering at max zoom levels using the disableClusteringAtZoom param below

markerHolder = new L.MarkerClusterGroup({
    disableClusteringAtZoom: 18,
    maxClusterRadius: 80,
    spiderfyDistanceMultiplier: 1,
})

I would like to add a button that clusters/unclusters on click

Best Answer

There is no 'uncluster' function to call per se, but you could keep both a clustered and unclustered version of your data available, and toggle between them when you click a button

var cluster = L.markerClusterGroup();
var marker = L.featureGroup();

for (var i = 0; i < points.length; i++) {
    var point = points[i];
    var marker = L.marker(new L.LatLng(point[0], point[1]));
    cluster.addLayer(marker);
    markers.addLayer(marker);
}

see: https://github.com/Leaflet/Leaflet.markercluster/issues/393

Related Question