angular-google-maps TypeError: $scope.map.control.refresh is not a function
angular-google-maps TypeError: $scope.map.control.refresh is not a function
我在我的 cordova 移动应用程序中集成了 angular-google-maps。我想使用以下函数刷新地图。
function refreshMap() {
$scope.map.control.refresh({
latitude: $scope.location.T_Lat_Deg_W.value,
longitude: $scope.location.T_Long_Deg_W.value
})
}
但是出现错误
angular.js:13540 TypeError: $scope.map.control.refresh is not a
function
at Scope.refreshMap (mapController.js:122)
at fn (eval at <anonymous> (angular.js:1), <anonymous>:4:224)
at expensiveCheckFn (angular.js:15475)
at callback (angular.js:25008)
at Scope.$eval (angular.js:17219)
at Scope.$apply (angular.js:17319)
at HTMLAnchorElement.<anonymous> (angular.js:25013)
at defaultHandlerWrapper (angular.js:3456)
at HTMLAnchorElement.eventHandler (angular.js:3444)
这里是这个问题的JSFiddle example。
有没有办法解决这个问题?谢谢!
其实这个问题的关键在于$scope.map.control.refresh
在地图加载完成之前不应该使用。如果地图最初是隐藏的,那么我会调用这样的函数
function refreshMap() {
//show map action
$scope.map.showMap = true;
//refresh map action
$scope.map.control.refresh({latitude: 48,longitude: 2.3});
}
refreshMap
函数会同时调用显示地图动作和刷新地图动作。这意味着当我调用 $scope.map.control.refresh 函数时地图没有完全加载。因此,它将报告 TypeError: $scope.map.control.refresh is not a function
.
一种方法是使用uiGmapIsReady
检测地图是否可以使用。
function refreshMap() {
//show map action
$scope.map.showMap = true;
//refresh map action
uiGmapIsReady.promise()
.then(function (map_instances) {
$scope.map.control.refresh({latitude: 48,longitude: 2.3});
});
}
这个JSFiddle使用uiGmapIsReady
来解决这个问题。
第二种方法是使用$timeout
延迟刷新动作。
function refreshMap() {
//show map action
$scope.map.showMap = true;
//delayed refresh map action
$timeout(function(){
$scope.map.control.refresh({latitude: 48,longitude: 2.3});
},3000);
}
这个JSFiddle用$timeout
来解决这个问题
我在我的 cordova 移动应用程序中集成了 angular-google-maps。我想使用以下函数刷新地图。
function refreshMap() {
$scope.map.control.refresh({
latitude: $scope.location.T_Lat_Deg_W.value,
longitude: $scope.location.T_Long_Deg_W.value
})
}
但是出现错误
angular.js:13540 TypeError: $scope.map.control.refresh is not a function
at Scope.refreshMap (mapController.js:122)
at fn (eval at <anonymous> (angular.js:1), <anonymous>:4:224)
at expensiveCheckFn (angular.js:15475)
at callback (angular.js:25008)
at Scope.$eval (angular.js:17219)
at Scope.$apply (angular.js:17319)
at HTMLAnchorElement.<anonymous> (angular.js:25013)
at defaultHandlerWrapper (angular.js:3456)
at HTMLAnchorElement.eventHandler (angular.js:3444)
这里是这个问题的JSFiddle example。
有没有办法解决这个问题?谢谢!
其实这个问题的关键在于$scope.map.control.refresh
在地图加载完成之前不应该使用。如果地图最初是隐藏的,那么我会调用这样的函数
function refreshMap() {
//show map action
$scope.map.showMap = true;
//refresh map action
$scope.map.control.refresh({latitude: 48,longitude: 2.3});
}
refreshMap
函数会同时调用显示地图动作和刷新地图动作。这意味着当我调用 $scope.map.control.refresh 函数时地图没有完全加载。因此,它将报告 TypeError: $scope.map.control.refresh is not a function
.
一种方法是使用uiGmapIsReady
检测地图是否可以使用。
function refreshMap() {
//show map action
$scope.map.showMap = true;
//refresh map action
uiGmapIsReady.promise()
.then(function (map_instances) {
$scope.map.control.refresh({latitude: 48,longitude: 2.3});
});
}
这个JSFiddle使用uiGmapIsReady
来解决这个问题。
第二种方法是使用$timeout
延迟刷新动作。
function refreshMap() {
//show map action
$scope.map.showMap = true;
//delayed refresh map action
$timeout(function(){
$scope.map.control.refresh({latitude: 48,longitude: 2.3});
},3000);
}
这个JSFiddle用$timeout
来解决这个问题