3

I'm using the Leaflet MarkerCluster plugin, and I want to know whether a particular cluster represents points at the same location, or whether the cluster would break into smaller components if I zoomed in.

By default, the cluster will "spiderfy" if it represents multiple points at the same location - I'd like to detect this scenario and handle the individual points myself, rather than spiderfying (using spiderfyOnMaxZoom: false).

pretty, but pretty useless too

There is an event fired when you click on a cluster icon. From the documentation:

markers.on('clusterclick', function (a) { // a.layer is actually a cluster console.log('cluster ' + a.layer.getAllChildMarkers().length); });

Is it possible to determine whether the cluster is at its maximum zoom level after the user has clicked on it? That is, if the user clicked on it again, would it continue to zoom in further, or does it represent points at the same location?

1

Ok, just did a quick test and was able to confirm that the animationend event will do the trick:

// markers is the markerClusterGroup
markers.on('animationend', function(a){
  console.log('animationend zoom level: ', map.getZoom())
})

markers.on('clusterclick', function(a){
  console.log('clusterclick zoom level: ', map.getZoom())
})

And the console confirmed it:

enter image description here

  • Many thanks for the tip. But I discovered an even easier method ;) – Stephen Lead Mar 22 '17 at 22:58
0

I found an easier method to determine this. If the layer has associated markers, it's at its maximum zoom level (this presumably is how the spiderfy function is able to display the individual markers).

So you can simply test for the presence of markers when the user clicks:

layer.on("clusterclick", function(a){ if (a.layer._markers.length > 0) { console.log("layer at max zoom"); } else { console.log("layer not at max zoom"); } });

  • Unfortunately, this may work for your particular situation, but does not in the general case: plnkr.co/edit/QgWlBupvtdpl2SE0uBEo?p=preview a.layer._markers contains the markers that get out of the cluster on the immediately higher zoom level, i.e. if you zoom in 1 extra level. But you can still have other child markers forming a cluster, hence the clicked cluster would not have spiderfied. – ghybs Apr 16 '18 at 9:39
  • FWIW I have proposed a solution there: stackoverflow.com/questions/49669565/… – ghybs Apr 16 '18 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.