Leaflet: openPopup() breaks setView() with animation

Created on 14 Aug 2015  路  9Comments  路  Source: Leaflet/Leaflet

Scenario is the following very common setup: clicking in sidebar to center map on marker and open marker's popup. What happens instead: map pans to wrong place and/or moves only till marker is in the map, but not in the center. It also happened that the popup got displayed in a totally different place than the marker on the screen.

Abstract code for trigger:

item_title_link.click(function() {
    map.panTo(layer.getLatLng(), {animate: true});
    layer.openPopup();
});

What happens:

  1. [bug]: When clicking for the first time, the map doesn't center on the marker, but pans until it is in the corner/edge of map. In this case, panBy is called twice.
  2. [good]: When clicking for the second time, the map centers on the marker. In this case, panBy is only called once.

The reason as far I was able to debug is the following. The following paths are all fighting to move the screen and end up in broken results many times.

  1. panTo -> setView -> _tryAnimatedPan -> panBy is always called. This is good and it is the required behaviour.
  2. Layer.openPopup -> Map.openPopup -> setLatLng -> update -> _adjustPan -> panBy. This should not happen, as it destroys the real panBy movement. This path is not happening once the marker is visible on the screen (at the edges/corners), thus the second time the activation happens this path doesn't run.

The only "fix" I found is to call setView with {reset: true} to completely disable animation.

Leaflet v1.0.0-beta.1

OS X 10.9.5, latest Chrome

bug

Most helpful comment

Have you tried to set the Popup {autoPan: false}?
I think I had a similar problem. Markers which were pretty much at the edge of the screen didn't center when a popup was bound to them. Setting this option solved the problem.

All 9 comments

Swapping the panTo and openPopup lines fixes the marker's position on the map but breakes the popup's position, which is now usually not visible (depending on the distance panned).

Have you tried to set the Popup {autoPan: false}?
I think I had a similar problem. Markers which were pretty much at the edge of the screen didn't center when a popup was bound to them. Setting this option solved the problem.

@clemenslermen brilliant, that's a nice workaround.

@clemenslermen thanks
it works for me.

@hyperknot do you still have the issue with latest master? If yes, can you set up a test case with http://playground-leaflet.rhcloud.com/ to reproduce it? Thanks :)

I'll have a look at reproducing it with master today.

Reproduced, still not fixed in master:
http://playground-leaflet.rhcloud.com/rur/3/edit?html,js,output

On the other hand, popup.openOn(map) got fixed in master (might be unrelated):
http://playground-leaflet.rhcloud.com/qor/1/edit?html,js,output

Just to confirm: OK in beta 2.

Was this page helpful?
0 / 5 - 0 ratings