In my case, I just wanted to set the zoom level to one less than what Google maps chose for me during fitBounds. The goal was to use fitBounds, but also to ensure that no markers were in any map tools, etc.
My map is created earlier, and then a number of other dynamic page components have the ability to add markers by calling fitBounds after each addition.
This is in the start block, where the map object was originally created ...
var mapZoom = null;
This is then added to each block where a marker is added, right before calling map.fitBounds ...
google.maps.event.addListenerOnce(map, 'bounds_changed', function() { if (mapZoom != map.getZoom()) { mapZoom = (map.getZoom() - 1); map.setZoom(mapZoom); } });
When using 'bounds_changed' without checking in place, the map is scaled once for each marker, regardless of whether it is needed or not. Conversely, when I used "zoom_changed", sometimes I had markers in the map tools because the scale did not change. Now it always works, but checking ensures that it only scales once and only if necessary.
Hope this helps.
oucil Sep 21 '12 at 13:45 2012-09-21 13:45
source share