0

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?

1
  • Maybe it is so easy but I'm new in AngularJS Commented Aug 31, 2015 at 8:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

So, I have to copy this code in my controller under the function getCurrentPosition?
You have to copy it when you have your myLatlng variable. Anyway, if you simply copy-paste this code i don't thin that is going to work. You need to edit according to your needs. My example works with "locality" field or "administrative area level 3" field.
under $scope.address I insert an alert that show address.. but I the alert show : "[object Object],[object Object],[object Object],[object Object]"
If you change your answer with a solution, I vote up.
Man, I post you documentation about geocoder.geocode. $scope.address now is the structure you want with all the information you need. If you have that alert it mean that you have successfully use the google reverse geocode api. developers.google.com/maps/documentation/javascript/geocoding Now you have to read this object and take what you need.
|

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.