简体   繁体   English

Google API for Map无法与Angular JS $ scope.data输入配合使用

[英]Google API for Map is not working with angular JS $scope.data Inputs

I have the following angular script. 我有以下角度脚本。 i am facing issue with the datatype of the data which i have received in success method of the http request. 我在http请求的成功方法中收到的数据的数据类型面临问题。 the data which has been received is used for the displaying current location on the google map using javascript available. 已接收到的数据用于使用可用的javascript在google地图上显示当前位置。 The problem is, the location marker and the map is not getting displayed for longitude and the latitude received. 问题是,位置标记和地图无法显示所接收的经度和纬度。 However if i hard code and pass the static values(static inputs are commented), the map and the place marker is getting displayed. 但是,如果我硬编码并传递静态值(注释了静态输入),则将显示地图和位置标记。 Kindly help me resolving this issue. 请帮助我解决此问题。 Thanks a lot in advance. 非常感谢。

var app = angular.module("myApp", []);

app.controller("myCtrl",function ($scope,$rootScope,$http) {    
      var city = 'Mumbai';
      $scope.uploadResultFinal= false;

      $http({
          method: 'POST', 
          url: '\HomeServlet2',
          headers: {
             'Content-Type': 'application/json'
          },   
          params: {
             City:city
          }
      });
      .success(function(data) {
          console.log(angular.toJson(data));                    
          $scope.data1= data.Addresses;
          $scope.latlang =[$scope.data1[2].Latitude,$scope.data1[2].Longitude];
          $scope.uploadResultFinal= true;
      })
      .error(function(data, status, header, config) {
          console.log('Error');
      });



      $scope.$watch('uploadResultFinal', function() {
          alert($scope.latlang[0]);
          alert($scope.latlang[1]);
          var l1 =$scope.latlang[0]; 
          var l2 =$scope.latlang[1]; 
          /* static values are commented.*/
          /*
          var l1= "19.72";
          var l2= "72.63";
          */
          var myCenter=new google.maps.LatLng(l1,l2);


          function initialize() {
              var mapProp = {
              center:myCenter,
              zoom:5,
              mapTypeId:google.maps.MapTypeId.ROADMAP
          };

          var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

          var marker=new google.maps.Marker({
            position:myCenter,
            animation:google.maps.Animation.BOUNCE
            });

          marker.setMap(map);

          var infowindow = new google.maps.InfoWindow({
            content:"You Are Here!"
          });

          infowindow.open(map,marker);
     }

        google.maps.event.addDomListener(window, 'load', initialize);


},true); 
   }
);

Just a guess, You could try adding $scope.$apply() in the success & error functions. 只是一个猜测,您可以尝试在成功与错误函数中添加$ scope。$ apply()。 Or better add a .finally() to the $http. 或者最好在$ http中添加.finally()。

 .finally(function(){ $scope.$apply(); }) 

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM