I am trying to zoom to a specific marker and open its popup using an external onclick event. To achieve this I need get the leaflet ID of the target marker, spiderfy the MarkerClusterGroup and call openPopup()
on my target marker. I am able to to do all of this, however it takes 2 clicks to complete. Is there a way to do this with one click?
1 Answer
It looks like what's happening here is that the spiderfy and popup are firing at the beginning of the zoom event (you will notice that if you click the button when it is only zoomed out one or two levels, the spiderfy arms will briefly appear), but then at the end of the zoom, the clustering is recalculated, so the spiderfy arms and popup disappear.
Fortunately, MarkerCluster has a zoomToShowLayer
method that will zoom into a layer in the cluster group and call a callback function at the end of the zoom. This will do what you want:
markers.zoomToShowLayer(target, function() {
target.__parent.spiderfy();
target.openPopup();
})
In fact, in this case, you don't even need to tell it to spiderfy, as it needs to spiderfy to show the marker anyway.
Updated fiddle:
-
Thanks a lot, that worked great. Not sure how I missed that method!– its30Commented Feb 19, 2016 at 2:34