Center/Set Zoom of Map to cover all visible Markers?

前端 未结 4 1446
孤街浪徒
孤街浪徒 2020-11-28 17:20

I am setting multiple markers on my map and I can set statically the zoom levels and the center but what I want is, to cover all the markers and zoom as much as possible hav

4条回答
  •  庸人自扰
    2020-11-28 17:57

    The size of array must be greater than zero. Οtherwise you will have unexpected results.

    function zoomeExtends(){
      var bounds = new google.maps.LatLngBounds();
      if (markers.length>0) { 
          for (var i = 0; i < markers.length; i++) {
             bounds.extend(markers[i].getPosition());
            }    
            myMap.fitBounds(bounds);
        }
    }
    

提交回复
热议问题