如何确保加载地图

Yas*_*ash 4 angular-google-maps

getGMap()返回Map对象的一个​​实例.如果映射未就绪,则此函数返回undefined.是否有事件或方式告诉Map已准备好并且调用getGMap肯定会返回Google Map?

谢谢

佳日

gor*_*rds 12

您可以使用uiGmapIsReady您的控制器内-看到IsReady文档.

uiGmapIsReady返回:
- 地图加载并准备好后的承诺
- 地图信息数组(我称之为map_instances)
- 数组长度取决于您在页面中加载了多少个地图
- 数组中的每个对象都包含谷歌地图对象

getGmap()在控制对象上使用ready,将如下所示:

HTML

<div ng-app="myApp" ng-controller="myController">
    <ui-gmap-google-map 
        center="map.center" 
        zoom="map.zoom" 
        control="map.control">
    </ui-gmap-google-map>
</div>
Run Code Online (Sandbox Code Playgroud)

CONTROLLER

myApp.controller('myController', function ($scope, uiGmapIsReady) {

    $scope.map = {
        center : {
            latitude: 37.7749295, 
            longitude: -122.4194155 
        },
        zoom : 14,
        control : {}
    };

    uiGmapIsReady.promise()
    .then(function (map_instances) {
        var map1 = $scope.map.control.getGMap();    // get map object through $scope.map.control getGMap() function
        var map2 = map_instances[0].map;            // get map object through array object returned by uiGmapIsReady promise
    });

});
Run Code Online (Sandbox Code Playgroud)

注意你如何通过两种方法访问地图对象:
- 通过map_instances数组(看起来像[map1Instance, map2Instance, ...])
- $scope.map.control.getGMap()只要你在你的html中定义了它$scope

的jsfiddle