googleway icon indicating copy to clipboard operation
googleway copied to clipboard

Exact search google_geocode

Open nbarsch opened this issue 6 years ago • 9 comments

Hi SymbolixAU,

Weird issue that came up in google_geocode:

If I geocode a semi-remote place that is close to a common name, google_geocode does the suggested search in google maps rather than an exact search.

Cobly, Benin for example. If I search Cobly, Benin in google maps it asks me if I mean Colby, Benin and returns the result for the whole country of Benin. google_geocode has the same behavior I'm assuming because the API has the same behavior.

Putting in "Cobly, Benin" with quotes for an exact search in google maps returns the desired result.

Is there a way to mimic this exact search behavior through googleway (can you do exact search in the API?)

Code:

google_geocode("Cobly, Benin ",key=googleapikey)

###Trying to put quotes around it returns the same result as before
google_geocode("'Cobly, Benin'",key=googleapikey)

nbarsch avatar Mar 22 '18 07:03 nbarsch

A couple of questions

  1. When you say Google Maps, are you talking about the actual website (maps.google.com) or the google_map() function?

  2. When I put Cobly, Benin (without quotes) into Google Maps website, it doesn't ask "if I mean xxx". Can you share a screenshot?

You may also be noticing the difference between the data that the Google Maps website uses to do searches, and that which Google exposes through the Geocoding API. They are not the same.

SymbolixAU avatar Mar 23 '18 00:03 SymbolixAU

image

VS.

image

google_geocode returns the first screenshot's result, returning only the country of Benin for:

google_geocode("Cobly, Benin",key=googleapikey)

$results
             address_components
1 Benin, BJ, country, political
  formatted_address
1             Benin 
###And so on with coordinates etc. 

If at all possible I'd like the second result. I understand the API and Google Maps are different, but is there a way to mimic the "" exact search behavior to return the second result?

nbarsch avatar Mar 23 '18 05:03 nbarsch

Oddly this works I've (just now since posting above) discovered:


google_geocode("\"Cobly, Benin\"",key=googleapikey)

$results
                                                                                                                                                   address_components
1 Cobly, Atakora Department, Benin, Cobly, Atakora Department, BJ, administrative_area_level_2, political, administrative_area_level_1, political, country, political
  formatted_address
1      Cobly, Benin

Solution:

input <- "Cobly, Benin"
google_geocode(input,key=googleapikey)

exact <- TRUE
if(isTRUE(exact)){
input <- paste0("\"",input,"\"")
}
google_geocode(input,key=googleapikey)


nbarsch avatar Mar 23 '18 05:03 nbarsch

Oddly, if I simply search for Cobly, Benin (without quotes) I get the correct result.

screen shot 2018-03-23 at 4 53 48 pm

But that's a good catch about adding quotes around the search string in google_geocode, very useful to know!

SymbolixAU avatar Mar 23 '18 06:03 SymbolixAU

Has google programmed more spelling corrections for Americans? That's hilarious.

nbarsch avatar Mar 23 '18 06:03 nbarsch

Also, since I don't correct it in the command, the quotes only seem to be helping me. In a list pulled off GADM.org shapefile Benin level 2 names, 3 extra were geocoded correctly(in the US :) ) with the quotes than without, without any loss.

nbarsch avatar Mar 23 '18 06:03 nbarsch

@nbarsch probably your first pass was wrong since '' are not interpreted the same as "" in Google Maps.

For Colby, Benin I get the same "suggested..." result on Google Maps (searching from Singapore), and the result's identical for 'Colby, Benin'.

"Colby, Benin" returns the correct result. If you hate escaping quotes like I do you can run google_geocode('"Colby, Benin"'), and the proposed exact argument can work as sprintf('"%s"', address)

MichaelChirico avatar May 07 '18 07:05 MichaelChirico

Another example of this

https://twitter.com/obrl_soil/status/1057558971173691392

google_geocode(address = "The Environmental & Analytical Laboratories, Wagga Wagga, NSW, Australia")

SymbolixAU avatar Oct 31 '18 09:10 SymbolixAU

What's really fun is if I escape the & like \\& I still get Santa Fe, but if I try \\\\& I get... Oklahoma.

obrl-soil avatar Oct 31 '18 09:10 obrl-soil