Is there an API that lists states and cities?

Asked

Viewed 23,926 times

21

I’ve been searching for some json API that lists the states of a particular country, or cities of a particular state, I’ve searched for this feature for several Google Maps Apis but I couldn’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 something quite heavy.

Note: the need is to create a combobox of countries, states and cities, but I can use a list of countries straight from the database without any problem, but if there is an API that provides this list is also better.

There’s a good option?

  • 1

    There are already good answers here about this, in particular for cities PT & BR. That suits you or want cities from all over the world?

  • Yes, I’m going to need a list of all the countries, and then I’m going to request a list of states, and cities for each state.

  • I’ve been looking for something similar and found this JSON on Github that may be useful.

  • 1

    I know it’s a bit old, but it can help others Use the IBGE API for this, more reliable source is impossible. Follow documentation: https://servicodados.ibge.gov.br/api/docs/localities

  • just for the record, da para usar a api do google maps também.

  • Not using BD is complicated by being dependent on third party API, I already have a migrate to cities and states I use in Laravel. For your case the IBGE solution mentioned above would be the best option.

  • I found another API with states and municipalities in Brazil that has been very useful to me, this one is: https://www.back4app.com/database/back4app/api-estados-cidades-brasil

Show 2 more comments

1 answer

27


You can use the public service geonames.org.

This example tab 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 daughter entities of 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 the selection of South America (Geonameid 6255150), gets the list of countries

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

  3. After the selection of Brazil (Geonameid 3469034), gets 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á"
    },
    [...]
  • 1

    This API lacks some cities, when selecting the state of São Paulo, you do not get some cities like Capital São Paulo, São Bernado do Campo and etc. Sâo Paulo

  • 1

    not missing, the API call limits the results

Browser other questions tagged

You are not signed in. Login or sign up in order to post.