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

后端 未结 4 1879
时光取名叫无心
时光取名叫无心 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:16

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

    Maps API v3

    1. get markers
    2. get boundaries of markers
    3. set center on map by fitting it to marker boundaries

    var markers = [markerObj1, markerObj2, markerObj3];
    
    var newBoundary = new google.maps.LatLngBounds();
    
    for(index in markers){
      var position = markers[index].position;
      newBoundary.extend(position);
    }
    
    map.fitBounds(newBoundary);
    

    The code above will automaticlly center and zoom your map so that all markers are visible.

提交回复
热议问题