Forked from vitaliy-kravchenko-dev/goggle-maps-extract-city-country.js
Created
February 19, 2024 12:45
-
-
Save omofolarin/180d11a629f2a1085fceb633e63f255b to your computer and use it in GitHub Desktop.
Extract city and country from Google maps API response - geocoder.geocode()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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