角度JS地理位置得到城市



我在app.js中有以下代码。我想在 $scope.city 变量中捕获位置城市。我已经在地理编码功能之外声明了城市,但是城市没有得到任何价值。需要您的帮助

// code as below 
app.controller('ydmoreCtrl', function($scope, $timeout, $ionicModal, $ionicSideMenuDelegate, Categories) {
 (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position){
      var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
      var geocoder = new google.maps.Geocoder();  
        var city; 
            geocoder.geocode({'latLng': latlng}, function(results, status)
                    {
                        if (status == google.maps.GeocoderStatus.OK)
                         {
                                if (results[0])
                                {
                                    var add= results[0].formatted_address ;
                                    var  value=add.split(",");
                                    count=value.length;
                                    country=value[count-1];
                                    state=value[count-2];
                                    city=value[count-3];
                                    callback(latlng);
                           //         alert(city);
                                }
                                else 
                                {
                          alert("address not found");
                                }
                        }
                    });
      $scope.$apply(function(){
            $scope.city = "Pune";

      });
    });
  }
/*

*/

你可以尝试调用

$scope.apply()

城市设置为

$scope.城市

内部到地理编码器.地理编码调用

最新更新