5

Tôi đang cố gắng đặt giới hạn vào phiên bản Google Maps nhưng không thành công. Tôi đang sử dụng mô-đun AngularJS và angular-google-maps. thử cuối cùng của tôi là:Đặt giới hạn Google Maps với 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> 

Trong app.js của tôi, tôi đã viết:

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

}); 

Chức năng maps.fitBounds() không tồn tại. Tôi thử cách tiếp cận $scope.map.bounds, nhưng không trả lời theo cách tôi nghĩ.

+0

Hãy nhìn vào [này] (https://github.com/angular-ui/angular-google-maps/blob/master /example/example.html) github, và cố gắng tìm kiếm 'giới hạn', có rất nhiều đoạn mã liên quan đến nó. – bjiang

+0

Tôi đã đọc tệp này, chưa thành công. Gist này chứa cách tiếp cận mới (dựa trên các tập tin được đề xuất) https://gist.github.com/vitorcarvalhoml/62f804cd197572f28079 Làm thế nào tôi có thể thiết lập map.center và map.zoom dựa trên giới hạn? –

Trả lời

2

Theo docs, giới hạn chỉ thị hơi khác với giới hạn bản đồ google. Bạn nên đặt rõ ràng các thuộc tính northeastsouthwest với các thuộc tính latitudelongitude riêng của chúng.

Dưới đây nên làm việc:

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

Và đặt giá trị này cho ví dụ trong các hằng số của Angular - gây ra bên trong bộ điều khiển nó ngừng hoạt động sau lần sử dụng đầu tiên. –

8

Nếu bạn đang sử dụng góc-google-map của "cột mốc" chỉ thị, chỉ cần thêm phù hợp = "true" thuộc tính (ui-GMap-dấu).

Ví dụ:

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

Nguồn: https://stackoverflow.com/a/29707965

Các vấn đề liên quan