how to get google maps coordinates from country code?

106 views Asked by At

Let's say I have a country code "US", how can I get back the coordinates of the United States of America using Google Map?

1

There are 1 answers

0
Yrll On BEST ANSWER

You Just use Geocoding and put the US as a value in the address parameter.

https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&address=US

And this should return the coordinates of US like so: 37.09024,-95.712891

Take note though that this does not work for all country codes because some are returning zero results like Japan as JP.

One thing you could do is just manually save the associated country names with their country code and use that full name to query in the Geocoding API instead of the country code only. Since it is considered best practice not to input ambiguous queries in the Geocoding API, and the country code only input would be against that practice.

Examples:

US - https://maps.googleapis.com/maps/api/geocode/json?key=API_KEY&address=United%20States

JP - https://maps.googleapis.com/maps/api/geocode/json?key=API_KEY&address=Japan

And this should work fine. I hope this helps!