4

How is it possible that map.getCenter() might be different from map.getBounds().getCenter()?

> cragMap.getCenter()
> Q {d: 13.823563748466814, e: 0, toString: function, b: function, equals: function…}
> cragMap.getBounds().getCenter()
> Q {d: 5.9865924355766005, e: 0, toString: function, b: function, equals: function…}

This happens in my case and prevents me from coding one particular feature. Any idea what is the cause of this?

clime
  • 8,695
  • 10
  • 61
  • 82
  • 1
    Presumably this is because getCenter() on the map object itself is applied on a global scale, whereas getCenter() on a LatLngBounds object will compute the center of a limited space – Stephen Wright Feb 05 '14 at 11:57
  • 1
    And with lower zoom level difference is rising. – Anto Jurković Feb 05 '14 at 12:06
  • possible duplicate of [goole maps API 3 zooms out on fitBounds](http://stackoverflow.com/questions/8170023/goole-maps-api-3-zooms-out-on-fitbounds) – geocodezip Feb 05 '14 at 14:40
  • @geocodezip: not a duplicate. Read the question carefully please. – clime Feb 05 '14 at 14:44

1 Answers1

6

It is caused by latitude non-linearity of mercartor projection. map.getBounds().getCenter() returns average of latDim and lngDim. But that average is usually different from the center of the map because to the north and south the scale of latitude changes.

clime
  • 8,695
  • 10
  • 61
  • 82
  • Thanks for the explanation! Any suggestions on how to work around this or in other words how to get the calculated map center for bounding box? – hugo der hungrige Sep 23 '21 at 12:40