Google Map Set Zoom Level


Sometimes we need to show all the markers in google map window if we are creating markers dynamically. Google had already thought about users this requirement, here is how you can do it.

Use the “GLatLngBounds” class to implement it:

var bounds = new GLatLngBounds();
bounds.extend(point1);
bounds.extend(point2);
bounds.extend(point13);
map.setCenter(bounds.getCenter());
map.setZoom(map.getBoundsZoomLevel(bounds));

OR

var bounds = new GLatLngBounds();

function zoomfit()
{
newzoom = getBoundsZoomLevel(bounds);
newcenter = bounds.getCenter();
map.setCenter (newcenter,newzoom);
}

function creatmarker()
{

var marker = new GMarker(point);
map.addOverlay(marker);
bounds.extend(point);
zoomfit();

}

Advertisements