How to set the Google Map zoom level to show all the markers?

后端 未结 4 1889
时光取名叫无心
时光取名叫无心 2020-12-08 00:36

How do I set the zoom level to show all the markers on Google Maps?

In my Google Map there are different markers in different positions. I want to set google map zoo

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 01:11

    You can use the extend method of the GLatLngBounds object, which represents a rectangle on the map.

    var bounds = new GLatLngBounds();
    

    Loop around all the points on your map, extending the bounds of your GLatLngBounds object for each one.

    bounds.extend(myPoint);
    

    Finally you can use your bounds object to set the centre and zoom of your map (where map is your map object)

    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    

提交回复
热议问题