views:

2430

answers:

3

I would like to pass an x amount of geo-locations to the Google Maps API and have it centered around these locations and set the appropriate zoom level so all locations are visible on the map. I.e. show all the markers that are currently on the map.

Is this possible with what the Google Maps API offers by default or do I need to resolve to build this myself?

+2  A: 

There are a number of ways of doing this but it is pretty easy if you are using the V2 API. See this post for an example, this group post or this post.

carson
Note that the first link mentions that getBoundsZoomLevel() is undocumented - this is no longer true. I implemented something similiar for a project of mine, it works well - except I wish I knew about GBounds.extend() then!
matt b
+2  A: 

For V3 there's zoomToMarkers

Andrew
+1  A: 

I used fitBounds (API V3) for each point:

1.Declare the variable.

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

2.Go through each point with FOR loop

var latlng = new google.maps.LatLng(COORDINATES);           
bounds.extend(latlng);
map.fitBounds(bounds);
gray