【发布时间】:2014-03-05 16:07:30
【问题描述】:
我得到了错误: 'angular-google-maps:找不到有效的中心属性'。 当我 $watch 从 php 后端加载的 locationData 并执行初始化函数时。
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', ['$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,
};
}]);
如何在加载数据后进行谷歌地图指令解析并使用后端 Lat、Lng 数据正确显示地图?
【问题讨论】:
标签: javascript angularjs google-maps google-maps-api-3 angular-google-maps