Google Maps API Tutorial

© 2006, 2007, 2008, 2009 Mike Williams

Translate this page:


Fitting the map to the data

The map.getBoundsZoomLevel() method allows us to calculate a zoom level setting that fits a set of markers.

Here's an example

It is necessary tp perform a map.setCenter() call before starting to add markers. At this point we don't know the real parameters for the call, so just use any values.

   map.setCenter(new GLatLng(0,0),0);
Create an empty GLatLngBounds() object.
   var bounds = new GLatLngBounds();
Each time a point is read, extend the bounds to include that point.
When all the points have been processed, the zoom level can be set to fit the points.
The centre can be obtained by using the bounds.getCenter() method

Potential Pitfalls

You can't mix v1 compatibility mode and v2 native syntax in this code. You do need to use a GLatLngBounds(), not a GBounds(), and you can only use a GLatLng in its .extend() method, not a GPoint.

Back to Mike's Homepage
Up to the start of this tutorial