Reputation: 715
If all I have is a city, such as "Centerport, New York" or "Barranquilla", how could I obtain the country of the city? If I get the lang/long of the city, from there, can I get the country based on the coordinates?
Upvotes: 2
Views: 5995
Reputation: 171
I've been tinkering about with something very similar, so I guess I can help.
Google Geocoding API handles this well. Make a request with the city's name and it includes the country in the response.
However, if there are multiple cities with the same name in different countries, it will return you ALL the cities that match the criteria; the approach to take in this case is to browse city by city and find the one that matches the correct country. Simple coding solves this issue, but even then there can be multiple cities in the same country that match the request;
I suggest that, if you have the coordinates, you make a direct request using them instead of using the names, since that will solve the multiple cities problem completely. You can make a request with the coordinates directly to the API. It works well. Here's an example.
Upvotes: 1