离子:反向地理编码 [英] Ionic: Reverse Geocoding

查看:85
本文介绍了离子:反向地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将GPS服务整合到我的应用程序中。我已经添加并注入了Cordova GPS插件。
我可以检索gps上的ngCordova文档中显示的经度和纬度。这是我的方法:

$ $ p $ .controller('geoLocationCtrl',function($ scope,$ window){
$ window.navigator.geolocation.getCurrentPosition(function(position){
$ scope。$ apply(function(){
$ scope.lat = position.coords.latitude;
$ scope.lng = position.coords.longitude;
})

console.log(position);
})
})

我的挑战是如何使用上述方式返回用户的当前地址,州和国家。

解决方案

将经度和纬度转换为人类可读地址的过程称为反向地理编码。许多地图API支持Google地图和Here地图。您可以使用Google Maps API完成此操作。



添加Google Maps JavaScript API

 < script type =text / javascriptsrc =http://maps.google.com/maps/api/js?sensor=false&language=en> ;< /脚本> 

查询API :这里是一个角度控制器样本


$ b $

  app.controller('geoLocationCtrl',function($ scope,$ window){
$ window.navigator.geolocation.getCurrentPosition( function(position){
$ scope。$ apply(function(){
$ scope.lat = position.Coordinates.latitude;
$ scope.lng = position.Coordinates.longitude;

var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng($ scope.lat,$ scope.lng);
var request = {
latLng:latlng
};
geocoder.geocode(request,function(data,status){
if(status == google.maps.GeocoderStatus.OK){
if(data [0]!= null){
alert(address is:+ data [0] .formatted_address);
} else {
alert(No地址可用);
}
}
})
console.log(position);
})
})
});


I am integrating GPS Services into my application. I have added and injected the Cordova GPS plugin. I am able to retrieve the longitude and the latitude as shown on the ngCordova documentation on gps. This is my approach:

.controller('geoLocationCtrl', function($scope,$window) {
    $window.navigator.geolocation.getCurrentPosition(function(position){
        $scope.$apply(function(){
            $scope.lat = position.coords.latitude;
            $scope.lng = position.coords.longitude;
        })

        console.log(position);
    })
})

My challenge is how can I use the above to return the current address, state and country of the user.

解决方案

The process of turning a longitude and latitude into a human readable address is called Reverse Geocoding. Many map APIs support that like Google Maps and Here Maps. Here is how you can do it using Google Maps API

Add Google Maps JavaScript API

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>

Query the API: here is an angular controller sample

app.controller('geoLocationCtrl', function($scope, $window) {
  $window.navigator.geolocation.getCurrentPosition(function(position) {
    $scope.$apply(function() {
      $scope.lat = position.Coordinates.latitude;
      $scope.lng = position.Coordinates.longitude;

      var geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng($scope.lat, $scope.lng);
      var request = {
        latLng: latlng
      };
      geocoder.geocode(request, function(data, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (data[0] != null) {
            alert("address is: " + data[0].formatted_address);
          } else {
            alert("No address available");
          }
        }
      })
      console.log(position);
    })
  })
});

这篇关于离子:反向地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆