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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s