I have a set of points I want to plot on an embedded Google Map (API v3). I\'d like the bounds to accommodate all points unless the zoom level is too low (i.e., zoomed out
google.maps.event.addListener(marker, 'dblclick', function () {
var oldZoom = map.getZoom();
map.setCenter(this.getPosition());
map.setZoom(parseInt(oldZoom) + 1);
});
Had the same problem, needed to fit many markers on the map. This solved my case:
bounds.extend(objLatLng)
)Execute fitbounds AFTER map is completed:
google.maps.event.addListenerOnce(map, 'idle', function() {
map.fitBounds( bounds );
});
Please try this:
map.fitBounds(bounds);
// CHANGE ZOOM LEVEL AFTER FITBOUNDS
zoomChangeBoundsListener = google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
if (this.getZoom()){
this.setZoom(15);
}
});
setTimeout(function(){
google.maps.event.removeListener(zoomChangeBoundsListener)
}, 2000);
If 'bounds_changed' is not firing correctly (sometimes Google doesn't seem to accept coordinates perfectly), then consider using 'center_changed' instead.
The 'center_changed' event fires every time fitBounds() is called, although it runs immediately and not necessarily after the map has moved.
In normal cases, 'idle' is still the best event listener, but this may help a couple people running into weird issues with their fitBounds() calls.
See google maps fitBounds callback
I use:
gmap.setZoom(24); //this looks a high enough zoom value
gmap.fitBounds(bounds); //now the fitBounds should make the zoom value only less
This will use the smaller of 24 and the necessary zoom level according to your code, however it probably changes the zoom anyway and doesn't care about how much you zoomed out.
this work's for me with API v3 but with setting fixed zoom:
var bounds = new google.maps.LatLngBounds();
// extend bounds with each point
gmap.setCenter(bounds.getCenter());
gmap.setZoom( 6 );