Mapbox-gl-js: map.getCenter().lat differs from map.getBounds().getCenter().lat

Created on 31 Oct 2017  路  2Comments  路  Source: mapbox/mapbox-gl-js

Not sure if this is a bug or if I'm missing something basic here. The docs certainly don't clarify anything in this regard. I was told that these values should be the same if I haven't set the other camera properties like tilt/rotation - which I haven't. Upon instantiating the map, for more zoomed in views like zoomLevel 9 etc, the lat values are the same. For more zoomed out views like zoomLevel 4, the lat values differ by a significant amount. The longitudes always match.

Here is a jsbin:
https://jsbin.com/ligipawoke/2/edit?html,console,output

The docs say:
map.getCenter(): Returns the map's geographical centerpoint.
map.getBounds(): Returns the map's geographical bounds.
bounds.getCenter(): Returns the geographical coordinate equidistant from the bounding box's corners.

Most helpful comment

This is expected behavior. map.getCenter().lat calculates the center of the map in screen coordinates, then converts that to geographical coordinates by applying the inverse of the Web Mercator projection. map.getBounds().getCenter().lat converts the corners of the map to geographic coordinates using the same formula, then finds the midpoint in both axes. But because the relationship between projected and unprojected latitudes is not linear in Web Mercator (and in Mercator projections in general), these are not the same value. Mercator projections "stretch" distances in the north-south direction the further you get from the equator.

All 2 comments

This is expected behavior. map.getCenter().lat calculates the center of the map in screen coordinates, then converts that to geographical coordinates by applying the inverse of the Web Mercator projection. map.getBounds().getCenter().lat converts the corners of the map to geographic coordinates using the same formula, then finds the midpoint in both axes. But because the relationship between projected and unprojected latitudes is not linear in Web Mercator (and in Mercator projections in general), these are not the same value. Mercator projections "stretch" distances in the north-south direction the further you get from the equator.

John, thanks for the explanation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jfirebaugh picture jfirebaugh  路  3Comments

PBrockmann picture PBrockmann  路  3Comments

stevage picture stevage  路  3Comments

rasagy picture rasagy  路  3Comments

Scarysize picture Scarysize  路  3Comments