With Leaflet markercluster, how can I switch between clustered and unclustered without changing zoom level?
Asked Answered
V

1

5

I am using the Leaflet.markercluster plugin to cluster my points.

However, users are asking for the functionality to switch between clustered and unclustered views (basically, they want to see all their points at a high zoom).

I know that I can disable clustering at a specific zoom level:

markers = new L.MarkerClusterGroup({
    disableClusteringAtZoom: 8,
})

How can I force everything to uncluster when I am further zoomed out, though?

Volitive answered 10/5, 2016 at 14:58 Comment(0)
A
11

You would probably be interested in Leaflet.MarkerCluster.Freezable plugin.

It provides extra methods on Leaflet.markercluster, including disableClustering() and enableClustering(), which enable you to uncluster / cluster programmatically (e.g. on a button click) without needing the user to zoom in/out.

See also the plugin demo page.

Of course, you should be careful whenever you call disableClustering(), since you may instantly load all your markers on map (if you are zoomed out to their full extent). If that tries loading thousands of markers, you may freeze your browser or even crash it.

Disclaimer: I am the author of this plugin.

Afroasiatic answered 10/5, 2016 at 15:58 Comment(1)
Thanks for sharing. I think I can implement enableClustering() and disableClustering() in my app on my own. ♥️Tenstrike

© 2022 - 2024 — McMap. All rights reserved.