5

An address containing a "#" (for example an apartment number) frequently gives an incorrect location result, both with ggmap::geocode and with google maps as well, so this is not strictly an R question. In this example, adding a "#3" after the street address changes the location result from Illinois to California:

> test <- geocode('1200 Davis St, Evanston, IL 60202', source='google', output='more')
> test[, c('lon', 'lat', 'administrative_area_level_1')]
        lon      lat administrative_area_level_1
1 -87.68978 42.04627                    Illinois

> testhash <- geocode('1200 Davis St #3, Evanston, IL 60202', source='google', output='more')
> testhash[, c('lon', 'lat', 'administrative_area_level_1')]     
        lon      lat administrative_area_level_1
1 -122.1692 37.72169                  California

If you experiment with google maps directly, sometimes adding a hash into an address seems to confuse the lookup, generating a variety of geographically dispersed results. This doesn't always happen, but in my experience happens frequently. It's easily fixed (there's no need for an apartment number when geocoding) but I'm wondering why it happens and if there are other cautions about entering addresses.

Robert McDonald
  • 1,250
  • 1
  • 12
  • 20
  • The # is a comment marker in R. Maybe the `geocode` function also treats it that way. Do you get the same result from `geocode('1200 Davis St', source='google', output='more')`? – user2554330 May 12 '18 at 09:58
  • @user2554330 Excellent suggestion and yes this seems to give the same wrong result. Experimenting just now I discovered that if you type "1200 Davis St #3" in Google Maps, maps autocomplete switches from local results to results where the zip code begins with the integer, in this case "3". But the California address zip begins with "9". So it may be that you're right and the behavior in geocode and in maps is due to a different mechanism. I think one would have to explore the geocode function to pin it down. – Robert McDonald May 12 '18 at 11:45
  • Would you be able to just strip out the # characters from the strings you're looking up? Not sure if this is data you have some control over – camille May 12 '18 at 14:33
  • @camille Thanks and yes, that's what I did. I was puzzled however about why it was necessary. – Robert McDonald May 12 '18 at 21:43

1 Answers1

2

Google has recommendations in regard to address formatting in Geocoding API. Particularly they suggest do not specify additional elements like apartment number or floor numbers in requests.

You can check the complete list of recommendations in Google Maps FAQ:

https://developers.google.com/maps/faq#geocoder_queryformat

The relevant part is

Do not specify additional address elements such as business names, unit numbers, floor numbers, or suite numbers that are not included in the address as defined by the postal service of the country concerned. Doing so may result in responses with ZERO_RESULTS.

I hope this helps!

xomena
  • 31,125
  • 6
  • 88
  • 117
  • Thanks.The link is very helpful! While this doesn't explain specifically what the hash is doing, it does clarify that Google's API is not designed to handle things like apartment numbers and that the hash is therefore likely to foul up the results. – Robert McDonald May 14 '18 at 15:33