Angular-Google-Map:如何在加载位置数据后加载地图(Lat,Lng)?

Che*_*Lin 7 javascript google-maps google-maps-api-3 angularjs angular-google-maps

我收到了错误:'angular-google-maps:找不到有效的中心属性'.当我看到我的locationData从php后端和exec初始化函数加载.

HTML:

<div ng-init="locationData = '<?=htmlspecialchars($data)?>'">
    <google-map center="map.center" zoom="map.zoom" options="map.options"></google-map>
</div>
Run Code Online (Sandbox Code Playgroud)

角度控制器:

app.controller('MapCtrl', ['$scope', function ($scope) {
    'use strict';

    $scope.$watch('locationData', function() {
        var location = JSON.parse($scope.locationData);
        $scope.location = {
            lng : location.longitude,
            lat : location.latitude
        initialize();
    });

    function initialize() {
        var mapOptions = {
            panControl    : true,
            zoomControl   : true,
            scaleControl  : true,
            mapTypeControl: true,
            mapTypeId     : google.maps.MapTypeId.ROADMAP
        };

        $scope.map = {
            center: {
                latitude: $scope.location.lat,
                longitude: $scope.location.lng
            },
            zoom: 13,
            options: mapOptions,
        };
    }
}]);
Run Code Online (Sandbox Code Playgroud)

并且,我将$ scope.map设置移动到控制器块的顶部,并将常量值设置为map

center: {
    latitude: 0,
    longitude: 0
},
Run Code Online (Sandbox Code Playgroud)

,地图显示正确.

角度控制器:

app.controller('MapCtrl', ['$scope', function ($scope) {
    'use strict';

    var mapOptions = {
        panControl    : true,
        zoomControl   : true,
        scaleControl  : true,
        mapTypeControl: true,
        mapTypeId     : google.maps.MapTypeId.ROADMAP
    };

    $scope.map = {
        center: {
            latitude: 0,
            longitude: 0
        },
        zoom: 13,
        options: mapOptions,
    };
}]);
Run Code Online (Sandbox Code Playgroud)

如何在加载数据后使用谷歌地图指令解析并使用后端Lat,Lng数据正确显示地图?

Che*_*Lin 9

最后,我使用了ng-if,并在map初始化之后将map.isReady值更改falsetrue.

<div ng-init="locationData = '<?=htmlspecialchars($data)?>'">
    <div ng-if="map.isReady">
        <google-map center="map.center" zoom="map.zoom" options="map.options"></google-map>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

有用.