zoom to accommodate all markers in google map

Recently I faced the issue to zoom to accommodate all markers in google map.In the google map, I included some polygons and lot of markers with landmarks and different locations.

Google Maps API v3 allows us to automatically center and zoom the map around a set of coordinates. It provides a LatLngBounds object to which we can add multiple LatLng objects. We can pass this to Map.fitBounds() function as below:

The trick is to add all points that into a google.maps.LatLngBounds object using the extend() method.
Like this we can extend as many as latlng as we want and after that we need to use these latlngbounds with the Map.fitBounds() function.

But with polygon,It’s little bit tricky not so hard. A polygon itself is just an array of coordinates that define a shape. So we need to extend latlngbound with every coordinate of polygon and then need to pass in Map.fitBounds() function.

Like this, we can zoom to accommodate all markers in google map. If yo have any query regarding this post or google map, you are welcome to post on our site.

Like this Post? Share