Michael
Michael

Reputation: 324

Reverse Geocoding AngularJS

I have to improve my code with reverse geocoding. Now my app get the position by device's GPS.

This is my controller:

.controller('MapCtrl', function($scope, $ionicLoading, $compile) {
  function initialise() {   
    function onSuccess(position){
      var myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
      var mapOptions = {
        zoom: 15, 
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP, 
      }
      var map = new google.maps.Map(document.getElementById('map'), mapOptions);
      var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
      });
      $scope.map = map; 
    }
    function onError(error){
      alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }
    navigator.geolocation.getCurrentPosition(onSuccess, onError);   
  }
  google.maps.event.addDomListener(window, 'load', initialise);

 });

I found in this link a solution, but I'm not be able to integrate it with my code.

Anyone can help me?

Upvotes: 0

Views: 2516

Answers (1)

Argo
Argo

Reputation: 335

This is the function to get address from lat and long.

$scope.geocoder.geocode({ 'latLng': $scope.latlng }, function (results, status)

And you need to include google api's:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&#038;sensor=false&#038;ver=2014-07-18"></script> 

After this you must handle the response. I post some example code.

$scope.geocoder.geocode({ 'latLng': $scope.latlng }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
    if (results[1]) {
        $scope.address = results[1].address_components;

        $scope.gps_data.address = $scope.address;
        //your code here


        // some example results parsing code
        var i;
        $scope.comune = undefined;
        for (i = 0; i < $scope.gps_data.address.length; i += 1) {
            if ($scope.gps_data.address[i].types.indexOf("administrative_area_level_3") > -1) {
                $scope.comune = $scope.gps_data.address[i].short_name;
                console.log($scope.comune);
            }
        }
        if ($scope.comune === undefined) {
            for (i = 0; i < $scope.gps_data.address.length; i += 1) {
                if ($scope.gps_data.address[i].types.indexOf("locality") > -1) {
                    $scope.comune = $scope.gps_data.address[i].short_name;
                }
            }
        }     

    } else {
        console.log('Location not found');
        $scope.firstExecution = false;
    }
} else {
       console.log('Geocoder failed due to: ' + status);
       $scope.firstExecution = false;
}
});

Anyway, you need to check this documentation for better knwoledgment https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

Here you can find all the tags about the object structure that you recieve from google's api.

Upvotes: 1

Related Questions