2

If I run the below codes directly, it gives me an error because the address is too specified, however if I remove -272, it works fine.

So how can I keep removing words automatically until the function runs and give me the address,

library(googleway)    
 google_geocode(address = "경북 경주시 외동읍 문산공단길 84-272", language = "kr", key = api_key,
AAA
  • 83
  • 8

1 Answers1

1

The API works for me if I use the address in your question. However, using the address in your other question gives me a ZERO_RESULTS return.

We can remove the last part(s) of the address after the final space using simple regex in a gsub() command.

library(googleway)
set_key("your_api_key")

## invalid query
add <- "대한민국 경기도 안산시 단원구 성곡동 강촌로 140"
res <- google_geocode(address = add, language = "kr")
res
# $results
# list()
# 
# $status
# [1] "ZERO_RESULTS"

## remove the last part after the final space and it works
new_add <- gsub(' \\S*$', '', add)

res <- google_geocode(address = new_add, language = "kr")
geocode_coordinates(res)
#        lat      lng
# 1 37.31737 126.7672

You can convert this into an iterative loop that will continue to remove everything after the final 'space' character and attempt to geocode on the new address.

## the curl_proxy argument is optional / specific for this scenario 
geocode_iterate <- function(address, curl_proxy) {

    continue <- TRUE
    iterator <- 1

    while (continue) {
        print(paste0("attempt ", iterator))
        print(address)
        iterator <- iterator + 1

        res <- google_geocode(address = address, language = "kr", curl_proxy = curl_proxy)
        address <- gsub(' \\S*$', '', address)

        if (res[['status']] == "OK" | length(add) == 0 | grepl(" ", add) == FALSE ){
            continue <- FALSE
        }
    }
    return(res)
}

add <- "대한민국 경기도 안산시 단원구 성곡동 강촌로 140"
res <- geocode_iterate(address = add, curl_proxy = curl_proxy)
# [1] "attempt 1"
# [1] "대한민국 경기도 안산시 단원구 성곡동 강촌로 140"
# [1] "attempt 2"
# [1] "대한민국 경기도 안산시 단원구 성곡동 강촌로"

Be careful to make sure the while loop CAN actually exit. You don't want to enter an infinite loop.

And remember that even though ZERO_RESULTS are returned, the query still counts towards your daily API quota.

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • because I am currently using work environment computer, my code is not working without your function "curl_proxy". I just tried to put "curl_proxy" in the geocode_iterate, it doesnt' run. Do you know any solution, combining curl_proxy to geocode_iterate?? – AAA Apr 03 '18 at 06:37
  • @Arvin I've updated the answer with what I *think* should be the solution. However, I can't test it – SymbolixAU Apr 03 '18 at 06:42
  • Thank you for the reply, I got an error message not related to curl_proxy but it says "Error: is.character(target_url) is not TRUE " and when I run with debug, debug location is "stopifnot(is.character(target_url))" from function (target_url = "http://www.google.com") code – AAA Apr 03 '18 at 07:43
  • AU my address appears like this °­ÃÌ·Î, ´Ü¿ø±¸, ¾È»ê½Ã, °æ±âµµ – AAA Apr 03 '18 at 07:53