Load City list for Country

with No Comments


Need to show two selectboxes to User:

  • Country
  • City

City select box should change its data based on Country

Possible solutions

Online Webservice

That was me fist idea: just find needed (possibly free!) API Service and use it from grails project. But after googling a lot I did not find any solution which allow to retrieve all cities of country in one API call. At all I did not find any API doing this.

The only API which looks similar was Geonames but it do not return cities of country but have children associations, like USA -> States -> Counties -> Cities. You need to call :

  1. http://ws.geonames.org/countryInfoJSON?country=US&formatted=true then take geonameId
  2. and execute http://api.geonames.org/childrenJSON?geonameId=6252001&username=demo&formatted=true
  3. doing step 2 until you get cities

This is not acceptable if I want to have only two selectboxes – Country and City

Database of Cities

Next solution was to take existing dataset from anywhere in any format and put it to database.

The most interesting free databases are:

  1. http://www.geodatasource.com/world-cities-database/free – It contain 2 million cities in text format, but file which contain it has mapping like CC_FIPS -> City and required transforming it into CountryCode (ISO3166-2) -> City based on mapping CC_FIPS -> CountryCode
  2. http://www.maxmind.com/app/worldcities – huge database of cities in csv format with lat,long for each city. Was dismissed as it is really huge – 3 millions of cities. I did not want to display all Country cities as it is too much – only 10 – 200 most important based on country. Really cool data but not usable for me
  3. http://mydatamaster.com/free-downloads/  (World Cities and Languages) – contains about 4000 cities with district/region and population info in SQL format. That was my case!  Small reprocessiong of SQL to change ISO3166-3 country codes into ISO3166-2 and that is it!

Of course in this case we do not show all cities and risk that data will be old in some years, but honestly I do not think that this will be the case for any time as in our changing world sites are re-written each 3 years minimum and info will be updated then.

Follow Michael Astreiko:

Latest posts from

Leave a Reply