select onchange应该改变地图边界

时间:2013-11-23 03:00:02

标签: javascript google-maps angularjs

我有一些区域选择(省级) - 如果用户选择任何区域 - 地图应该更改中心并缩放以最适合该区域。这就是理论。现在代码:

controller.js:

KPNControllers.controller('branchesCtrl', ['$scope', 
  function branchesCtrl($scope) {

    $scope.voivodships = [
        {"name" : 'dolnośląskie', "bounds" : {
               "northeast" : {
                  "lat" : 51.8047592,
                  "lng" : 17.7989169
               },
               "southwest" : {
                  "lat" : 50.09634,
                  "lng" : 14.816831
               }
            }},
        {"name" : 'kujawsko-pomorskie', "bounds" : {
               "northeast" : {
                  "lat" : 53.7809988,
                  "lng" : 19.7618465
               },
               "southwest" : {
                  "lat" : 52.33079009999999,
                  "lng" : 17.2472673
               }
            }}];
   $scope.$watch('voivodship', function() {
      if ($scope.voivodship !== undefined) {
        console.log($scope.voivodship.bounds);
        $scope.bounds = new google.maps.LatLngBounds(new google.maps.LatLng($scope.voivodship.bounds.northeast),new google.maps.LatLng($scope.voivodship.bounds.southwest));
        console.log($scope.bounds);
        $scope.map.panToBounds($scope.bounds);
        $scope.map.fitBounds($scope.bounds);
      }
    })

部分/ branches.html:

<select ng-model="voivodship" ng-options="voivodship.name for voivodship in voivodships" class="full-width"><option value="">-- wybierz województwo --</option></select>

不幸的是地图没有改变,控制台显示:

RangeError: Maximum call stack size exceeded

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

首先 - 您不能将$ scope.voivodship.bounds.northeast用作LatLng的来源 - 它是有用的,但您必须使用新的google.maps.LatLng($ scope.voivodship.bound.northeast.lat,$ scope.voivodship.bound.northeast.lng)(也许有人可以让它变得更好?)

第二个 - 仔细查看doc - LatLngBounds构造函数:https://developers.google.com/maps/documentation/javascript/reference?hl=pl&csw=1#LatLngBounds

LatLngBounds(sw?:LatLng,ne?:LatLng)

首先是不是 - 如果你做错了 - 它会把你送到太空; D

正确的代码:

 $scope.$watch('voivodship', function() {
    if ($scope.voivodship !== undefined) {
      $scope.bounds = new google.maps.LatLngBounds(new  google.maps.LatLng($scope.voivodship.bounds.southwest.lat,$scope.voivodship.bounds.southwest.lng), new google.maps.LatLng($scope.voivodship.bounds.northeast.lat,$scope.voivodship.bounds.northeast.lng));
        $scope.map.panToBounds($scope.bounds);
        $scope.map.fitBounds($scope.bounds);
  }}