我将 angular-google-maps 集成到我的 cordova 移动应用程序中。我想使用以下函数刷新 map 。
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
不应该在 map 加载完成之前使用。如果 map 最初是隐藏的,那么我会调用这样的函数
function refreshMap() {
//show map action
$scope.map.showMap = true;
//refresh map action
$scope.map.control.refresh({latitude: 48,longitude: 2.3});
}
refreshMap
函数会同时调用显示 map Action 和刷新 map Action 。这意味着当我调用 $scope.map.control.refresh 函数时 map 没有完全加载。因此,它将报告 TypeError: $scope.map.control.refresh is not a function
。
一种方法是使用uiGmapIsReady
检测 map 是否准备好使用。
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
来延迟刷新 Action 。
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
来解决这个问题。
关于javascript - angular-google-maps TypeError : $scope. map.control.refresh 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36704139/