5

Próbuję ustawić granice w instancji Google Maps, ale bez powodzenia. Używam modułu AngularJS i angular-google-maps. Moja ostatnia próba była:Ustaw granice w Mapach Google za pomocą map kątowych google

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

w moim app.js pisałem:

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

}); 

Funkcja maps.fitBounds() nie istnieje. Próbuję zastosować podejście $scope.map.bounds, ale nie zareagowałem tak, jak myślę.

+0

Take wygląd w [W] (https://github.com/angular-ui/angular-google-maps/blob/master /example/example.html) github, i spróbuj wyszukać 'bounds', jest dużo kawałka kodu z nim związanego. – bjiang

+0

Przeczytałem ten plik, bez powodzenia. Ta część zawiera nowe podejście (oparte na sugerowanym pliku) https://gist.github.com/vitorcarvalhoml/62f804cd197572f28079 Jak ustawić map.center i map.zoom na podstawie granic? –

Odpowiedz

2

Zgodnie z docs, granice dyrektyw różnią się nieznacznie od ograniczeń map Google. Powinieneś jawnie ustawić atrybuty northeast i southwest z własnymi latitude i longitude.

Poniżej powinno działać:

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

I umieść te wartości na przykład w stałych Angulara - ponieważ wewnętrzny kontroler przestaje działać po pierwszym użyciu. –

8

Jeśli używasz "markery" dyrektywą kątowej-google-map, tuż dodać dopasowanie = "true" atrybutu (UI-GMap-markerów).

przykład:

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

Źródło: https://stackoverflow.com/a/29707965

Powiązane problemy