show by text in which city I am angular js

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

Are you in Barranquilla / Cartagena or something

Controller:

.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)
      });
    });
  }
});
 2
Author: gugadev, 2016-09-25

1 answers

You can use Google Maps to accomplish your task.

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 goes your Google Maps key.

Then you create a Geocoder to get your location and you get the locality keys for 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);
      }
    }
  }
}
 1
Author: gugadev, 2016-09-26 13:20:53