javascript - angular-google-maps TypeError : $scope. map.control.refresh 不是函数

标签 javascript angularjs cordova google-maps angular-google-maps

我将 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/

相关文章:

javascript - 创建类似于移动 Gmail 的固定容器

C# Webbrowser 文档,invokemember ("click"),是否可以等到该单击操作得到解析?

javascript - 为动态变化的对象创建一个工厂/服务,并且可以将其传递给 Controller

android - navigator.geolocation.getCurrentPosition 与 Cordova

android - Cordova Android 应用程序在拍摄照片时因 RecoverableSecurityException 而崩溃

javascript - 删除和添加 TextGeometry 会卡住场景

javascript - 使用 JavaScript 检查文本输入模式是否有效

javascript - 为什么当我告诉 AngularJS $scope.watch() 观察数组时它停止工作?

css - 为图库创建缩略图

javascript - 需要使应用程序宽度适合设备宽度