googleway
googleway copied to clipboard
Exact search google_geocode
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)
A couple of questions
-
When you say Google Maps, are you talking about the actual website (maps.google.com) or the
google_map()
function? -
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.
VS.
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?
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)
Oddly, if I simply search for Cobly, Benin
(without quotes) I get the correct result.
But that's a good catch about adding quotes around the search string in google_geocode
, very useful to know!
Has google programmed more spelling corrections for Americans? That's hilarious.
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 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)
Another example of this
https://twitter.com/obrl_soil/status/1057558971173691392
google_geocode(address = "The Environmental & Analytical Laboratories, Wagga Wagga, NSW, Australia")
What's really fun is if I escape the & like \\&
I still get Santa Fe, but if I try \\\\&
I get... Oklahoma.