Edi Junior
Edi Junior

Reputation: 53

How can I get only the city name from geocoder?

I'm a very very very beginner in javascript. I'm building an app and decided to implement some geolocation features, the main one is to show the user his current locaition. I did it, but my function returns a full adress (like street, number, neighborhood, city, zip code and country) This is what is done so far...

/* CURRENT LOCATION */
function geolocationSuccess(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;

  var geocoder = new google.maps.Geocoder();
  var latlng = {lat: latitude, lng: longitude};

  geocoder.geocode({'location': latlng}, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      if (results[0]){
        var user_address = results[0].formatted_address;
        document.getElementById("current_location").innerHTML = user_address;
      }else {
        console.log('No results found for these coords.');
      }
    }else {
      console.log('Geocoder failed due to: ' + status);
    }
  });
}

function geolocationError() {
  console.log("please enable location for this feature to work!");
}

$(document).ready(function() {
  if ("geolocation" in navigator) {
    navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError);
  } else {
    alert("Geolocation not supported!");
  }
});

How can I get and show just the city?

Upvotes: 0

Views: 3251

Answers (2)

Radonirina Maminiaina
Radonirina Maminiaina

Reputation: 7004

The result has address_components property. And it contains city, zip, etc ...

For example, in the official documentation, this is the content of address_components:

[
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ]

If you want to get locality for example, just do

 result[0].address_components.filter(address => address.types.includes('locality'))[0].long_name

Upvotes: 1

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23409

Because you're returning the full address:

var user_address = results[0].formatted_address;

instead you need to iterate thru the address components and find the locality:

var user_city = results[0].address_components.filter(ac=>~ac.types.indexOf('locality'))[0].long_name

The data structure is shown here: https://developers.google.com/maps/documentation/geocoding/intro

Upvotes: 4

Related Questions