Ionic: reverse geocoding

I integrate GPS services in my application. I added and introduced the Cordova GPS plugin. I can get longitude and latitude as shown in 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 task is how I can use the above to return the current address, state and country of the user.

+4
source share
2 answers

The process of turning longitude and latitude into a human readable address is called Reverse Geocoding . Many map APIs support features such as Google Maps and Maps. Here's how you can do it using the Google Maps API.

API JavaScript Google

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

API: angular

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);
    })
  })
});
+8

, lat long: Google.

0

Source: https://habr.com/ru/post/1624756/


All Articles