Google Maps API Tutorial© 2006, 2007 Mike Williams |
Fitting the map to the dataThe 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. bounds.extend(latlng);When all the points have been processed, the zoom level can be set to fit the points. map.setZoom(map.getBoundsZoomLevel(bounds));The centre can be obtained by using the bounds.getCenter() method map.setCenter(bounds.getCenter()); Potential PitfallsYou 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. |