Monday, December 18, 2017

Getting city from geocode results Google Maps Api

Leave a Comment

I am using the Google Maps API (https://maps.googleapis.com/maps/api/geocode/xml) to geocode places and addresses. However, the results seem inconsistent. The administrative_area_level_2 and locality OR the locality and sublocality_level_1 seem to overlap. But, depending on the address, the overlap is different. One of the things I need to do is determine the city. The documentation states that the locality is the equivalent of a city.

If I geocode "Noord-Holland, Nederland" which is a province I get:

  • country:Nederland
  • administrative_area_level_1: Noord-Holland

I can understand. All seems well...

If I geocode "Amsterdam, Noord-Holland, Nederland" I get the following result:

  • country:Nederland
  • administrative_area_level_1: Noord-Holland
  • administrative_area_level_2:Amsterdam
  • locality:Amsterdam

Here I start not understanding the result. Why is Amsterdam listed as an administrative_area_level_2 as well as a locality? But ok, Amsterdam is the locality, thus the city. Which is correct.

If I geocode "Anton de Komplein 150, 1102 CW Amsterdam, Noord-Holland, Nederland" I get the following results:

  • country:Nederland
  • administrative_area_level_1:Noord-Holland
  • administrative_area_level_2:Amsterdam
  • locality:Amsterdam-Zuidoost
  • sublocality_level_1:Amsterdam-Zuidoost

Now I really don't understand.

  1. Here is Amsterdam-Zuidoost mentioned twice
  2. Why is the locality here "Amsterdam-Zuidoost"? It is not the city... I would have assumed that "Amsterdam" would be the locality and "Amsterdam-Zuidoost" the sublocality_level_1.

Can someone explain how i should interpretate the results?

2 Answers

Answers 1

I've had a look at your examples and I believe these are data issues that should be reported to Google.

Let's explain my findings:

The request for 'Amsterdam, Noord-Holland, Nederland'

https://maps.googleapis.com/maps/api/geocode/json?address=Amsterdam%2C%20Noord-Holland%2C%20Nederland&key=MY_API_KEY

returns the following address components for me

"address_components":[     {       "long_name":"Amsterdam",       "short_name":"Amsterdam",       "types":[         "locality","political"       ]     },     {       "long_name":"Government of Amsterdam",       "short_name":"Government of Amsterdam",       "types":[         "administrative_area_level_2","political"       ]     },     {       "long_name":"North Holland",       "short_name":"NH",       "types":[         "administrative_area_level_1","political"       ]     },     {       "long_name":"Netherlands",       "short_name":"NL",       "types":[         "country","political"       ]     }   ] 

Note that administrative_area_level_2 has a name 'Government of Amsterdam'. I understand you might be using the Dutch language to get results

https://maps.googleapis.com/maps/api/geocode/json?address=Amsterdam%2C%20Noord-Holland%2C%20Nederland&language=nl&key=MY_API_KEY

In this case I have:

{     "long_name":"Amsterdam",     "short_name":"Amsterdam",     "types":[       "locality","political"     ] }, {     "long_name":"Amsterdam",     "short_name":"Amsterdam",     "types":[       "administrative_area_level_2","political"     ] } 

So at this point it looks like the Dutch version is not translated correctly, we have 'Government of Amsterdam' in English and 'Amsterdam' in Dutch. This is a data issue. By the way you can see the administrative area level 2 here:

https://www.google.com/maps/place/Government+of+Amsterdam,+Amsterdam,+Netherlands/@52.3544136,4.7585038,11z/data=!3m1!4b1!4m5!3m4!1s0x47c63fb5949a7755:0xe4f20c148755745!8m2!3d52.3666969!4d4.8945398

The second example is a bit more complicated.

https://maps.googleapis.com/maps/api/geocode/json?address=Anton%20de%20Komplein%20150%2C%201102%20CW%20Amsterdam%2C%20Noord-Holland%2C%20Nederland&key=MY_API_KEY

First of all you should be aware that for street address level searches you will get address components and formatted address in local language as described in the localization of street addresses article. So doesn't matter what language we specify English or Dutch we will see the Amsterdam as the administrative area level 2 and this is exactly the same data issue that we faced in the first example, it should be translated to 'Regering van Amsterdam' on Google side.

Referring to the locality component. It's really weird that Amsterdam-Zuidoost is defined as locality in Google database. I can see it as shown in the screenshot and it looks like a sublocality:

enter image description here

https://www.google.com/maps/place/Amsterdam-Zuidoost,+Netherlands/@52.2569773,4.995519,12z/data=!4m5!3m4!1s0x47c60c069aab8879:0x249b64218cad1744!8m2!3d52.3079989!4d4.9715451

However if you check this feature in the Geocoder tool, it clearly states that this is a locality feature:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#place_id%3DChIJeYirmgYMxkcRRBetjCFkmyQ

Note the pace ID is ChIJeYirmgYMxkcRRBetjCFkmyQ. I believe this also might be a data issue on Google side.

I would suggest sending a feedback to Google data team following the Google Maps help center:

https://support.google.com/maps/answer/3094045?hl=en&ref_topic=3093612

I hope my answer clarifies your doubts!

Answers 2

If a country like Netherlands had province's administrative_area_level_1 and administrative_area_level_2 would probably both be set. It's just a matter of how the country set itself up in my opinion.

The sublocality_level_1 is just a neighborhood or area inside the city, while the locality is your current location. From all of the locations listed sublocality_level_1 is the most precise when it comes to locating you so then that is your locality as well.

You could check if sublocality_level_1 is set and if it is then check the highest number in administrative_area_level_number and use that for country otherwise use locality.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment