Skip to content

Instantly share code, notes, and snippets.

@zainaali
Last active February 21, 2018 07:41
Show Gist options
  • Save zainaali/9e83e66cff93c6476dbe3794aedfa3f7 to your computer and use it in GitHub Desktop.
Save zainaali/9e83e66cff93c6476dbe3794aedfa3f7 to your computer and use it in GitHub Desktop.
Extract city from google autocomplete address api
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
//Extract city from autocomplete address api
var city;
for(var a = 0; a < place.address_components.length; a += 1) {
var addressObj = place.address_components[a];
for(var b = 0; b < addressObj.types.length; b += 1) {
if (addressObj.types[b] === 'locality') {
city = addressObj.long_name; // confirm that this is the country name
//console.log(addressObj.long_name); // confirm that this is the country name
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment