Skip to content

Conversation

jfirebaugh
Copy link
Member

I would expect the following to work:

var map = L.map('map');

map.on("moveend", function () {
  map.getBounds();
});

map.setView([51.505, -0.09], 13);

Instead, it produces the error "Set map center and zoom first."

Fiddle: http://jsfiddle.net/pTaeU/2/

@danzel
Copy link
Member

danzel commented Oct 3, 2012

This isn't totally an easy one to handle.
Map.js _resetView is where the issue comes from as we don't set _loaded until the very end.

We could move up the !_loaded block to be straight after viewreset (or the block above that maybe). We'd also probably want to stop firing movestart/zoomstart before _loaded is set, which would mean unbalanced movestart/end and zoomstart/end events on the initial load.

For now if you really need to add listeners before setting the initial view you could wrap them in a load listener like this:

var map = L.map('map');

map.on('load', function () {
  map.on("moveend", function () {
    map.getBounds();
  });
});

map.setView([51.505, -0.09], 13);

@mourner Need your thoughts on this one.

@jfirebaugh
Copy link
Member Author

How about this?

@mourner
Copy link
Member

mourner commented Oct 5, 2012

Looks good to me. Dave?

@danzel
Copy link
Member

danzel commented Oct 5, 2012

Sweet, merge it then :)
Was just worried there might be a reason it wa in that order.

@mourner
Copy link
Member

mourner commented Oct 5, 2012

If there was I no longer remember it :)

mourner added a commit that referenced this pull request Oct 5, 2012
Order-of-initialization problem with setView()/getBounds()/moveend
@mourner mourner merged commit ec16ea8 into Leaflet:master Oct 5, 2012
emartinez-usgs pushed a commit to emartinez-usgs/Leaflet that referenced this pull request Feb 27, 2013
emartinez-usgs pushed a commit to emartinez-usgs/Leaflet that referenced this pull request Aug 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants