Angular Google Maps fitBounds

时间:2017-05-10 22:50:10

标签: angularjs google-maps

我无法弄清楚如何从angular-google-maps实例调用fitBounds()。我试图在uiGmapGoogleMapApi结算后调用它,但我不知道在何处/如何访问此方法。从vanilla js实现中你可以调用map.fitBounds(bounds)但是我不确定angular-google-maps v2.2.1中的等价物是什么,感谢您的帮助。

修改

我通过将控制属性添加到scope.map然后在uiGmapIsReady解析时调用getGMap()来实现此功能。

      uiGmapGoogleMapApi.then(function(maps) {
        scope.bounds = new maps.LatLngBounds();
        scope.map = {
          center: {
            latitude: 47.60427,
            longitude: -122.33825
          },
          zoom: 16,
          polys: [],
          bounds: {},
          control: {} // add this property

        };
        traceVehicle(scope.tripDetails);
      });
      // add uiGmapIsReady and call getGMap()
      uiGmapIsReady.promise(1).then(function(instances) {
        instances.forEach(function(inst) {
          var map = inst.map;
          var uuid = map.uiGmap_id;
          var mapInstanceNumber = inst.instance; // Starts at 1.
          var gMap = scope.map.control.getGMap(); // assign getGMap();
          gMap.fitBounds(scope.bounds);
        });
      });



  function traceVehicle(selectedTrip) {
    var rawPolys = [];
    var polylineObj = {
      id: 1,
      path: [],
      stroke: {
        color: '#6060FB',
        weight: 3
      },
      editable: false,
      draggable: false,
      geodesic: true,
      visible: true
    };
    scope.polylines = [];
    if(selectedTrip.locations.length > 0) {
      angular.forEach(selectedTrip.locations, function(v, i) {
        polylineObj.path.push({ latitude: v.lat, longitude: v.lon });
        scope.bounds.extend({ lat: v.lat, lng: v.lon });
      });
      rawPolys.push(polylineObj);
      scope.map = {
        center: {
          latitude: polylineObj.path[(Math.round(polylineObj.path.length/2) -1)].latitude,
          longitude: polylineObj.path[(Math.round(polylineObj.path.length/2) -1)].longitude
        },
        zoom: 16,
        polys: rawPolys
      };
      scope.map.fitBounds(scope.bounds); // how to call fitBounds()?

1 个答案:

答案 0 :(得分:0)

您需要$scope.map成为Google地图对象。目前它只是选项。

var mapOptions = {
    zoom: 16,
    polys: [],
    bounds: {},
    center: new google.maps.LatLng(47.60427, -122.33825),
    mapTypeId: google.maps.MapTypeId.TERRAIN
};

$scope.map = new google.maps.Map(document.getElementById('map'), mapOptions);
$scope.map.fitBounds(YOUR_CODE_FOR_BOUNDS);