SINTEF-9012 / PruneCluster

Fast and realtime marker clustering for Leaflet
MIT License
551 stars 131 forks source link

Cluster/uncluster and Spiderify/unspiderify events #152

Open cnatis opened 6 years ago

cnatis commented 6 years ago

First off let me thank you for your good work!

Secondly, I am trying to catch the event when a cluster changes size (ie zoom out, cluster grows, zoom in, cluster splits) so I can handle my custom popups appropriately (close the popup when the marker is now part of a cluster). They aren't leaflet popups so I am losing out on that behaviour that comes prebaked with them.

I also want to do the same for unspiderify (close the popup when the cluster is unspiderified). I could get away with doing all this on zoom, but I don't want to close popups that are still valid (marker is not in cluster/its on its own), and I prefer not to have to do the calculations for what is in each cluster after the zoom.

Ideally these events would broadcast the affected cluster(s) with the affected markers.

fungiboletus commented 6 years ago

Hei, thanks :-)

We have such events implemented sadly. I don't think we have enough logic and data so far to get this kind of events. Unlike Leaflet.markercluster, the clusters are not split or merged depending on the zoom level. The algorithm generates brand new clusters for every update. Some clusters are "recycled" though.

You could add such events in PruneCluster, but it's a lot of work. Doing the calculations for each cluster after the zoom is perhaps not too bad.

You have to listen to zoom and move events from leaflet (and also your events if your markers move). Then for each marker with an open popup, you could check if the marker is inside the bounds of the clusters displayed on the map. You can get the list of clusters and their bounds. It's a loop inside a loop, but if you don't have too many markers and clusters (depending on the grid size), it should be fine. You could probably improve a lot the performances by doing something more clever, but I'm not sure it's necessary.

cnatis commented 6 years ago

@yellowiscool Hmm, ok I see what you mean about having enough logic to provide the extra information about the clusters given that we may be dealing with a small data set or a very large one. That part would probably have to be handled in zoom and move events from leaflet with some custom calculations.

I think the spiderify and unspiderify events should be easy enough to add in given that spiderify knows what markers and cluster is being spiderified, and an unspiderify event would have the details of the target cluster since we know any markers in that cluster would be now hidden. This is useful for interactions with a spiderified cluster such as automatically zooming to a cluster, spiderifying, then opening a popup on one of the markers, then closing that popup when the client does something to cause an unspiderify.