setbounds on google maps api v3

msaspence picture msaspence · Mar 18, 2010 · Viewed 11.8k times · Source

I'm trying to set the bounds of a map fitbounds doesnt work because it puts some space around the bounds therefore doing

{{{map.fitBounds(map.getBounds())}}}

a few times will quickly zoom the map out

I need to be able to do

{{{map.setBounds(map.getBounds())}}}

and for nothing to happen

please note I am using v3 of the api and therefore do not have access to getBoundsZoomLevel

Answer

Lester S picture Lester S · Aug 10, 2010

If I'm not mistaken, I'm assuming you want all your points to be visible on the map with the highest possible zoom level. I accomplished this by initializing the zoom level of the map to 16(not sure if it's the highest possible zoom level on V3).

var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 16
                                        , center: marker_point
                                        , mapTypeId: google.maps.MapTypeId.ROADMAP
});

Then after that I did the bounds stuff:

var bounds = new google.maps.LatLngBounds();

//you can have a loop here of all you marker points
//begin loop
bounds.extend(marker_point);
//end loop

map.fitBounds(bounds);

Result: Success!