微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我收到了错误
‘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>

角度控制器:

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,};
    }
}]);

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

center: {
    latitude: 0,longitude: 0
},

,地图显示正确.

角度控制器:

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

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

    $scope.map = {
        center: {
            latitude: 0,longitude: 0
        },};
}]);

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

解决方法

最后,我使用ng-if,并在map初始化后将map.isReady值从false更改为true.
<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>

有用.

原文地址:https://www.jb51.cc/js/156849.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐