【问题标题】:R, googleway, matching address by removing wordR,googleway,通过删除单词匹配地址
【发布时间】:2018-09-11 09:40:53
【问题描述】:

如果我直接运行下面的代码,它会给我一个错误,因为地址太指定了,但是如果我删除 -272,它就可以正常工作。

那么在函数运行并给我地址之前,我怎样才能自动删除单词,

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

【问题讨论】:

    标签: r api google-maps geolocation googleway


    【解决方案1】:

    如果我使用您问题中的地址,API 对我有用。但是,使用your other question 中的地址会给我一个ZERO_RESULTS 回报。

    我们可以在gsub() 命令中使用简单的正则表达式删除最后一个空格之后的地址的最后一部分。

    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
    

    您可以将其转换为一个迭代循环,该循环将继续删除最后一个“空格”字符之后的所有内容,并尝试对新地址进行地理编码。

    ## 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] "대한민국 경기도 안산시 단원구 성곡동 강촌로"
    

    小心确保while 循环能够真正退出。您不想进入无限循环。

    请记住,即使返回了 ZERO_RESULTS,该查询仍会计入您的每日 API 配额。

    【讨论】:

    • 因为我目前正在使用工作环境计算机,如果没有您的函数“curl_proxy”,我的代码将无法运行。我只是试图将“curl_proxy”放在 geocode_iterate 中,它没有运行。你知道任何解决方案,将 curl_proxy 与 geocode_iterate 结合起来吗??
    • @Arvin 我已经用我 认为 的解决方案更新了答案。但是,我无法测试它
    • 感谢您的回复,我收到一条与 curl_proxy 无关的错误消息,但它显示“错误:is.character(target_url) is not TRUE”,当我使用调试运行时,调试位置为“ stopifnot(is.character(target_url))" 来自函数 (target_url = "google.com") 代码
    • 我的地址看起来像这样 °Ì·Î, ´Ü¿ø±¸, ¾È»ê½Ã, °æ±âµµ
    猜你喜欢
    • 2018-01-29
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多