Search code examples
phpgeonames

Parsing region code from php-world-cities-array


I have pulled Cities array from https://github.com/tapvt/php-world-cities-array. Each region is abbreviated except in case of US

array (
  'city' => 'Sant Julià de Lòria',
  'region' => '06',
  'country' => 'AD',
  'latitude' => '42.46372',
  'longitude' => '1.49129',
)

Since many countries have multiple cities with same name, I need to distinguish based on regions. In order to display region, I need something more than numbers to be recognised by users. For US, region comes in proper manner but for other countries like Germany, its numbers which is unfortunately not human readable.

My question is how can I get human readable names for regions in the php-world-cities-array?


Solution

  • The region codes identify the administrative zones. For example, DE-01 identifies the Baden-Württemberg area, and here's the corresponding record:

      array (
        'city' => 'Stuttgart',
        'region' => '01',
        'country' => 'DE',
        'latitude' => '48.78232',
        'longitude' => '9.17702',
      )
    

    To decipher Region Code 01 to Baden-Württemberg, you'll have to build another array. You may use a reverse geocoding service such as the Google Maps API. For example:

    https://maps.googleapis.com/maps/api/geocode/json?address=48.78232,9.17702&hl=en&sensor=false
    

    This will give you all the names in each political administrative level. The Zone ID should be mapped to the province level, or "administrative_level_1". In the above example, this resolves to "Baden-Württemberg" or "BW" if you use the short_name field.

    Remember to build an array as you decode the regions. Otherwise there will be too many duplicate requests to the Maps API and you'll get rate limited.