show by text in which city I am angular js

2

I'm doing an app on ionic where I want to show the user where the city is. With this code I locate it but I need to show you something for example:

  

You are in Barranquilla / Cartagena or something like that

Driver:

.controller('CtrlUbi', function($scope) {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      $scope.$apply(function() {
        $scope.position = position; //Obtenemos info de la localizaicon
        console.log(position.coords.latitude);
        console.log(position.coords.longitude);
        console.log(position)
      });
    });
  }
});
    
asked by frd 25.09.2016 в 22:22
source

1 answer

1

You can use Google Maps to achieve your goal.

Requirements

You add Google Maps to your document:

<script src="https://maps.google.com/maps/api/js?key=TU_LLAVE"></script>

NOTE: Where it says TU_LLAVE obviously your Google Maps key goes.

Then you create a Geocoder to get your location and you get the keys locality of each result (you get several).

navigator.geolocation.getCurrentPosition(position => {
  const latitude = position.coords.latitude;
  const longitude = position.coords.longitude;
  const geocoder = new google.maps.Geocoder();
  let location;

  location = new google.maps.LatLng(latitude, longitude);
  geocoder.geocode({ 'latLng': location }, (results, status) => {
    processLocation(results);
  });
});

function processLocation(location) {
  if (location[1]) {
    for (var i = 0; i < location.length; i++) {
      if (location[i].types[0] === "locality") {
        let city = location[i].address_components[0].short_name;
        let state = location[i].address_components[2].short_name;
        let country = location[i].address_components[3].long_name;

        console.log(city, state, country);
      }
    }
  }
}
    
answered by 25.09.2016 / 23:18
source