Fixing this problem in 2017
Shrink the bounding box slightly before sending it to map.fitBounds()
.
Here's a function to shrink a bounding box:
// Values between 0.08 and 0.28 seem to work
// Any lower and the map will zoom out too much
// Any higher and it will zoom in too much
var BOUNDS_SHRINK_PERCENTAGE = 0.08; // 8%
/**
* @param {google.maps.LatLngLiteral} southwest
* @param {google.maps.LatLngLiteral} northeast
* @return {Array[google.maps.LatLngLiteral]}
*/
function slightlySmallerBounds(southwest, northeast) {
function adjustmentAmount(value1, value2) {
return Math.abs(value1 - value2) * BOUNDS_SHRINK_PERCENTAGE;
}
var latAdjustment = adjustmentAmount(northeast.lat, southwest.lat);
var lngAdjustment = adjustmentAmount(northeast.lng, southwest.lng);
return [
{lat: southwest.lat + latAdjustment, lng: southwest.lng + lngAdjustment},
{lat: northeast.lat - latAdjustment, lng: northeast.lng - lngAdjustment}
]
}
Use it like this:
// Ensure `southwest` and `northwest` are objects in google.maps.LatLngLiteral form:
// southwest == {lat: 32.79712, lng: -117.13931}
// northwest == {lat: 32.85020, lng: -117.09356}
var shrunkBounds = slightlySmallerBounds(southwest, northeast);
var newSouthwest = shrunkBounds[0];
var newNortheast = shrunkBounds[1];
// `map` is a `google.maps.Map`
map.fitBounds(
new google.maps.LatLngBounds(newSouthwest, newNortheast)
);
I have an app doing the same thing as codr: saving the current map bounds in the URL, and initializing the map from the URL bounds upon refresh. This solution works wonderfully for that.
Why does this work?
Google Maps essentially does this when fitBounds()
is called:
- Center the map on the centerpoint of the given bounding box.
- Zoom in to the highest zoom level where the viewport would contain the given bounds inside of the viewport box.
If the bounds given would exactly match the viewport, Google Maps doesn't consider that as "containing" the bounds, so it zooms out one more level.