Search code examples
google-maps-api-3fitbounds

google maps API 3 zooms out on fitBounds


I've run into a problem where calling map.fitBounds seems to zoom out. I'm trying to use the backbone.js router to save the map bounds in the url. I want to be able to bookmark the url and later have the map look exactly the same.

I noticed calling map.fitBounds(map.getBounds()) in the console will always zoom out. Where as I would like it to not change the map.

Is this normal behaviour? How can I enforce this so the map will look the same from one step to the next?

Thanks


Solution

  • This problem of Google Map API was discussed few times. See:

    Function map.fitBounds() sets the viewport to contain the bounds. But map.getBounds() function returns the viewport bounds with some extra margin (note that this is normal behaviour and no bug). The enlarged bounds no longer fits in the previous viewport, so the map zooms out.

    A simple solution to your problem is to use the map center and the zoom level instead. See functions map.getCenter(), map.setCenter(), map.getZoom() and map.setZoom().