2

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?

JsFiddle here

5

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:

http://jsfiddle.net/nathansnider/n6zxgkdn/7/

  • Thanks a lot, that worked great. Not sure how I missed that method! – its30 Feb 19 '16 at 2:34

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.