how do I get the zoom level after fitBounds

3,611 views
Skip to first unread message

fly2279

unread,
Jan 11, 2010, 10:52:45 PM1/11/10
to Google Maps JavaScript API v3
I am placing markers on the map in a loop and extending the bounds of
the map to fit the markers. When I use getZoom() to get the zoom level
after fitting the bounds it's the same as the initial value for the
map. My initial zoom value is set to 8 and that's what's returned
after fitting the bounds. I placed a listener on zoom_changed and
getZoom() returns 10 after fitting the bounds. How do I get the zoom
level after fitting the bounds since calling getZoom() immediately
after doesn't seem to return the correct value?

Chad Killingsworth

unread,
Jan 12, 2010, 7:54:08 AM1/12/10
to Google Maps JavaScript API v3
I encountered this problem as well. As far as I can tell, you have to
get the proper zoom value inside your zoom_changed event listener.

I got around that by setting a flag value then adding my markers and
calling fitBounds. Inside my zoom_changed listener, if the flag value
was set, I reset it then called another function to continue
processing.

Hope that helps,

Chad Killingsworth

fly2279

unread,
Jan 12, 2010, 10:00:29 AM1/12/10
to Google Maps JavaScript API v3
I've tried setting a flag variable before the loop that adds markers
and then resetting the variable to false afterwards. It's like the
zoom_level doesn't actually get changed until a short time after all
the markers are placed. I can use setTimout to 100 and then get the
proper zoom_level returned after placing the markers but I have a
feeling that amount of time will change with a change in number of
markers being placed. Can someone shed some light on what I'm doing
wrong?

On Jan 12, 6:54 am, Chad Killingsworth

Chad Killingsworth

unread,
Jan 12, 2010, 2:16:55 PM1/12/10
to Google Maps JavaScript API v3
You have to reset the flag value inside the zoom_changed event
listener - not in your original function. Here's a rough example of
how I would do it:

var FindCorrectZoom = false;
function AddMarkers() {
for(var i = 0; i < 10; i++ {
AddMarker(i);
}
FindCorrectZoom = true;
map.fitBounds(MarkerBounds);
}

function processFitBoundsZoomChanged(currentZoom) {
//do something here
}

function zoom_changed_listener() {
if(FindCorrectZoom) {
FindCorrectZoom = false;
processFitBoundsZoomChanged(this.getZoom());
}
}
google.maps.event.addListener(map, 'zoom_changed',
zoom_changed_listener);

I used this technique in my code to limit the maximum zoom level a
call to fitBounds could use.

Chad Killingsworth

fly2279

unread,
Jan 12, 2010, 3:40:38 PM1/12/10
to Google Maps JavaScript API v3
Thank You! That works perfectly.

On Jan 12, 1:16 pm, Chad Killingsworth

fly2279

unread,
Jan 12, 2010, 11:12:41 PM1/12/10
to Google Maps JavaScript API v3
Well it did work but not consistently. If I alert() the value of
this.getZoom() in the zoom_changed listener it gives me the correct
value. I save the value in a variable and when I take it out the alert
() it doesn't give the correct value. It's like it needs to wait 1
millisecond to actually load the correct value after fitbounds is
called. Any suggestions?

On Jan 12, 1:16 pm, Chad Killingsworth
<chadkillingswo...@missouristate.edu> wrote:

mattia berlusconi

unread,
Jan 13, 2010, 3:13:54 AM1/13/10
to google-map...@googlegroups.com
I had the same problem. My solution is (it works):

google.maps.event.addListener(map, 'zoom_changed', function() {//ascoltatore sull'evento zoom della mappa
zoomChangeBoundsListener = google.maps.event.addListener(map,'bounds_changed',function (event) {  
      //here I have the correct zoom level     
google.maps.event.removeListener(zoomChangeBoundsListener);
});
});

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.




Reply all
Reply to author
Forward
0 new messages