2013-11-23 43 views
0

我有一个选择与一些地区(voivodship) - 如果用户选择任何 - 地图应该改变中心和缩放以最适合该地区。这是理论。现在代码:选择onchange应改变地图范围

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 

任何想法?

回答

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:经纬度, ne?:LatLng)

首先是sw不是ne - 如果你做错了 - 它会将你送入太空; 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); 
    }} 
+0

有时,最好去睡觉或散步,并用新鲜的头脑可以追溯到; d –