5

我想设置边界到谷歌地图实例,但没有成功。我正在使用AngularJS和angular-google-maps模块。我最后的尝试是:设置谷歌地图边界与角谷歌地图

<div id="map_canvas"> 
    <ui-gmap-google-map 
     center="map.center" 
     zoom="map.zoom" 
     bounds="map.bounds" 
     options="options"> 

     <div ng-repeat="d in devicesMarkers track by d.id"> 

      <ui-gmap-marker 
       idkey="d.id" 
       coords="d.center" 
       options="d.options"> 
      </ui-gmap-marker> 
      <ui-gmap-circle 
       center="d.center" 
       stroke="d.circle.stroke" 
       fill="d.circle.fill" 
       radius="d.circle.radius" 
       visible="d.options.visible"> 
      </ui-gmap-circle> 

     </div> 

    </ui-gmap-google-map> 
</div> 

在我app.js我写道:

(...) 
.controller('TaihMapController', ['$scope', '$log', 'uiGmapGoogleMapApi', function($scope, $log, GoogleMapApi) { 
    $scope.devicesMarkers = devices; 
    $scope.map = { 
     center: { latitude: devices[0].center.latitude, longitude: devices[0].center.longitude }, 
     zoom: 12, 
     bounds: {} 
     // fit: true, 
    }; 
    GoogleMapApi.then(function(maps) { 
     $log.debug(maps); 
     var myBounds = new maps.LatLngBounds(); 
     for (var k = 0; k < devices.length; k++) { 
      var _tmp_position = new maps.LatLng(
       devices[k].center.latitude, 
       devices[k].center.longitude); 
      myBounds.extend(_tmp_position); 
     } 
    $scope.map.bounds = myBounds; 
    $scope.googleVersion = maps.version; 
    // $scope.bounds = myBounds; 
    // $scope.zoom = maps.getZoom(); 
    // maps.fitBounds(myBounds); 
    // $scope.bounds = maps.getBounds(); 

}); 

功能maps.fitBounds()不存在。我尝试了$scope.map.bounds的方法,但没有回应我的想法。

+0

采取看看[此](https://github.com/angular-ui/angular-google-maps/blob/master /example/example.html)github,并尝试搜索'bounds',有很多相关的代码段。 – bjiang

+0

我读了这个文件,但没有成功。这个要点包含了新的方法(基于建议的文件)https://gist.github.com/vitorcarvalhoml/62f804cd197572f28079如何根据边界设置map.center和map.zoom? –

回答

2

根据docs,指令边界与谷歌地图边界略有不同。您应该明确地设置northeastsouthwest属性与他们自己的latitudelongitude

下面应该工作:

$scope.map.bounds = { 
    northeast: { 
     latitude: myBounds.getNorthEast().lat(), 
     longitude: myBounds.getNorthEast().lng() 
    }, 
    southwest: { 
     latitude: myBounds.getSouthWest().lat(), 
     longitude: myBounds.getSouthWest().lng() 
    } 
}; 
+0

并将这个值例如放在Angular的常量中 - 导致内部控制器在首次使用后停止工作。 –

8

如果您正在使用角谷歌地图的 “标志” 的指令,只需添加适合= “true” 属性(UI-GMAP-标记)。

实施例:

<ui-gmap-google-map center="map.center" zoom="map.zoom" options="map.options"> <ui-gmap-markers fit="true" models="markers" coords="'self'"></ui-gmap-markers> </ui-gmap-google-map>

来源: https://stackoverflow.com/a/29707965