json – Is there any API that lists states and cities?

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:

  1. Get a list of continents

    http://www.geonames.org/childrenJSON?geonameId=6295630

  2. After selecting South America ( GeoNameID 6255150), get the list of countries

    http://www.geonames.org/childrenJSON?geonameId=6255150

  3. After selecting Brazil ( GeoNameID 3469034), get the list of states

    http://www.geonames.org/childrenJSON?geonameId=3469034

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á"
    },
    [...]
Scroll to Top