Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setCenter doesn't work when called before setZoom #5345

Closed
sheley1998 opened this issue Sep 22, 2017 · 1 comment
Closed

setCenter doesn't work when called before setZoom #5345

sheley1998 opened this issue Sep 22, 2017 · 1 comment

Comments

@sheley1998
Copy link

mapbox-gl-js version:

Steps to Trigger Behavior

var map = new mapboxgl.Map
({
container: 'somecontainer',
style: 'mapbox://styles/mapbox/streets-v9'
});

map.setCenter([121.472644, 31.231706]);
map.setZoom(10);

Expected Behavior

Map should be centered in Shanghai

Actual Behavior

Map centers near an Indonesian island called Tumbulawa

Fix (Hack?)

Calling setZoom before setCenter fixes this problem

@jfirebaugh
Copy link
Contributor

Hi @sheley1998. As you've discovered, calling setZoom first is the solution. The reason that order matters in this case is that when the map is zoomed out, the center location is constrained by the fact that we don't want to have blank space at the top or bottom of the map view. Thus setCenter is not guaranteed to set the center latitude to the value provided. When the map is zoomed in, this isn't an issue.

Eventually we're planning to remove setCenter and setZoom in favor of an API where you provide all desired camera settings in a single call (#2801). That will avoid this issue.

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

No branches or pull requests

2 participants