0

I have a page which displays different polygons saved as strings in SQL Db. I managed to center the map depending on the polygon being loaded, but I would also like to set the zoom level of the map, depending on the polygon's zoom level. I have seen many discussions on this topic (i.e. How can I get zoom levels to decode a polyline from Google Maps Directions API?), but I am nowhere nearer the solution. Could anyone help?

I center the map using coordinates, which I get from decodePath method,

google.maps.geometry.encoding.decodePath(googlePolygon)

however it only returns lat long values, without any levels or zoom level information

Community
  • 1
  • 1
Bartosz
  • 4,542
  • 11
  • 43
  • 69

1 Answers1

1

Process the path returned from google.maps.geometry.encoding.decodePath(googlePolygon) to create a bounds object for it, then use google.maps.Map.fitBounds with that bounds.

var polyPath = google.maps.geometry.encoding.decodePath(googlePolygon);
var bounds = new google.maps.LatLngBounds();
for (var i=0; i < polyPath.length; i++) {
  bounds.extend(polyPath[i]);
}
map.fitBounds(bounds);
geocodezip
  • 158,664
  • 13
  • 220
  • 245