4

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.

2 Answers 2

8

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);
    })
  })
});
Sign up to request clarification or add additional context in comments.

8 Comments

Using the above answer the application shows a white screen. See edited for the updated code.
you have code errors in your snippet, first access the variables from $scope in this line var latlng = new google.maps.LatLng($scope.lat, $scope.lng); And you have a couple of missing brackets at the end. I will edit my answer with the full code
No alert was triggered, whether address was returned or not returned but the long and lat displays
can you debug the code and tell me where is it failing for you? do you get any response at all from the geocode call?
with no errors in console? can you please elaborate on the behaviour you are facing so that I can help?
|
0

you need to find out the address and other details using the lat and long values: How to get address location from latitude and longitude in Google Map.?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.