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

Question:

I've been looking for some json API that lists the states of a certain country, or cities of a certain state, I searched for this feature for 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 heavy.

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's also even better.

Is there a good option?

Answer:

You can use the geonames.org public service.

This example page shows calls to the service:

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 GeoNameID entities of a given GeoNameID – countries in a continent, states in a country, cities in 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), you get the country list

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

  3. After selecting Brazil ( GeoNameID 3469034), you 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