Question:
I was looking for some json API that lists the states of a certain country, or cities of a certain state, I looked for this functionality through several Google Maps APIs but I didn't find anything that was exactly for this.
I don't want to use a DB with a list of states and cities because I believe it lacks flexibility and is quite cumbersome.
Note: the need is to create a combobox of countries, states and cities, but I can use a list of countries directly from the database without any problem, but if there is an API that makes this list available it is also even better.
Is there a good option?
Answer:
You can use the geonames.org public service.
This example page shows the service calls:
http://vikku.info/programming/geodata/geonames-get-country-state-city-hierarchy.htm
The specific function you want to use is Children , which returns collections of child entities for a given GeoNameID
– countries of a continent, states of a country, cities of a state, and so on.
The example above makes the following calls:
-
Get a list of continents
-
After selecting South America ( GeoNameID 6255150), get the list of countries
-
After selecting Brazil ( GeoNameID 3469034), get the list of states
The end result is a list similar to the example below:
{
"totalResultsCount":27,
"geonames":[
{
"geonameId":3665474,
"toponymName":"Acre"
},
{
"geonameId":3408096,
"toponymName":"Alagoas"
},
{
"geonameId":3407762,
"toponymName":"Amapá"
},
[...]