2017-05-10 70 views
0

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

编辑

我得到这个通过添加控件的属性来scope.map,然后调用getGMap()时uiGmapIsReady解析工作。

 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()? 
+0

'scope.map'只是一个对象。你想为'scope.map'创建一个'google.maps.Map'对象。至少从你分享的片段... – Searching

回答

0

您需要将$scope.map设为谷歌地图对象。目前这只是选择。

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);