Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vitaliy-kravchenko-dev/a97b17603b183dc4a6713ecbbdd09988 to your computer and use it in GitHub Desktop.
Save vitaliy-kravchenko-dev/a97b17603b183dc4a6713ecbbdd09988 to your computer and use it in GitHub Desktop.
Extract city and country from Google maps API response - geocoder.geocode()
getCityCountry(results) {
const location: any = {};
for (const component of results[0].address_components) {
if (component.types.includes('sublocality') || component.types.includes('locality')) {
location.city = component.long_name;
} else if (component.types.includes('administrative_area_level_1')) {
location.state = component.short_name;
} else if (component.types.includes('country')) {
location.country = component.long_name;
location.registered_country_iso_code = component.short_name;
}
}
return location;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment