Labels

Monday, May 31, 2010

Google Maps: Best Fit Zoom Level and Center Point.

If you are working with Google maps and have an array of latitude longitude points to plot as markers on map, then you might need to calculate the best fit zoom level and map center point so that all the lat lng points can accommodate comfortably. Here's the code snippet:
// map: an instance of GMap2
// latlng: an array of instances of GLatLng
var latlngbounds = new GLatLngBounds();
for (var i = 0; i < latlng.length; i++)
{
latlngbounds.extend(latlng[i]);
}
map.setCenter(latlngbounds.getCenter(),map.getBoundsZoomLevel(latlngbounds));

For further reference:
http://911-need-code-help.blogspot.com/2009/03/zoom-to-fit-all-markers-polylines-or.html