Angular Google Maps - automatically set 'center' and 'zoom' to fit in all markers

前端 未结 5 1604
暗喜
暗喜 2021-02-19 05:51

I have a dynamically generated list of markers within my Google Map. I want the map\'s center to be the center of all the markers and zoomed out just enough so that all markers

5条回答
  •  滥情空心
    2021-02-19 06:45

    Thanks to all the replies, I got this code which is working perfectly for me:

    var bounds = new google.maps.LatLngBounds();
    for (var i = 0, length = $scope.map.markers.length; i < length; i++) {
      var marker = $scope.map.markers[i];
      bounds.extend(new google.maps.LatLng(marker.latitude, marker.longitude));
    }
    $scope.map.control.getGMap().fitBounds(bounds);
    

    I hope it will help to someone.

提交回复
热议问题