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

前端之家收集整理的这篇文章主要介绍了javascript – Angular-Google-Map:如何在加载位置数据后加载地图(Lat,Lng)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到了错误
‘angular-google-maps:无法找到有效的中心地产’.
当我观察我的locationData从PHP后端和exec初始化函数加载时.

HTML

  1. <div ng-init="locationData = '<?=htmlspecialchars($data)?>'">
  2. <google-map center="map.center" zoom="map.zoom" options="map.options"></google-map>
  3. </div>

角度控制器:

  1. app.controller('MapCtrl',['$scope',function ($scope) {
  2. 'use strict';
  3.  
  4. $scope.$watch('locationData',function() {
  5. var location = JSON.parse($scope.locationData);
  6. $scope.location = {
  7. lng : location.longitude,lat : location.latitude
  8. initialize();
  9. });
  10.  
  11. function initialize() {
  12. var mapOptions = {
  13. panControl : true,zoomControl : true,scaleControl : true,mapTypeControl: true,mapTypeId : google.maps.MapTypeId.ROADMAP
  14. };
  15.  
  16. $scope.map = {
  17. center: {
  18. latitude: $scope.location.lat,longitude: $scope.location.lng
  19. },zoom: 13,options: mapOptions,};
  20. }
  21. }]);

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

  1. center: {
  2. latitude: 0,longitude: 0
  3. },

,地图显示正确.

角度控制器:

  1. app.controller('MapCtrl',function ($scope) {
  2. 'use strict';
  3.  
  4. var mapOptions = {
  5. panControl : true,mapTypeId : google.maps.MapTypeId.ROADMAP
  6. };
  7.  
  8. $scope.map = {
  9. center: {
  10. latitude: 0,longitude: 0
  11. },};
  12. }]);

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

解决方法

最后,我使用ng-if,并在map初始化后将map.isReady值从false更改为true.
  1. <div ng-init="locationData = '<?=htmlspecialchars($data)?>'">
  2. <div ng-if="map.isReady">
  3. <google-map center="map.center" zoom="map.zoom" options="map.options"></google-map>
  4. </div>
  5. </div>

有用.

猜你在找的JavaScript相关文章