我正在尝试从Google Geocoding API获取纬度/经度,但是当地址包含丹麦本地字符时,请求会失败。我怀疑是因为httr::GET函数对URL进行了编码,但我不确定自己是否正确。如果您直接将此链接复制/粘贴到浏览器中,您将获得有效结果:http://maps.googleapis.com/maps/api/geocode/json?address=Søholmen+9,+4500+Denmark 但是下面的代码无效,即使URL在解析为GET函数之前相同。如果我使用不带本地字符的地址,它就可以工作。
library(httr)
library(jsonlite)
library(stringr)
address <- "Søholmen 9, 4500 Denmark"
# address <- "Kronprinsesse Sofies Vej 6, 2000 Denmark"
base_url <- "http://maps.googleapis.com/maps/api/geocode/json?"
# An address OR components
geo_url <- paste0(base_url, "address=", str_replace_all(address, pattern = " ", replacement = "+"))
# Get the result
# get the content
# Parse the JSON
temp_geo_results <- httr::GET(url = URLencode(URL = geo_url), verbose())
temp_geo_results <- httr::content(temp_geo_results, as = "text")
temp_geo_results <- jsonlite::fromJSON(temp_geo_results)
这是我的sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Danish_Denmark.1252 LC_CTYPE=Danish_Denmark.1252 LC_MONETARY=Danish_Denmark.1252
[4] LC_NUMERIC=C LC_TIME=Danish_Denmark.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringr_0.6.2 jsonlite_0.9.10 httr_0.5
loaded via a namespace (and not attached):
[1] RCurl_1.95-4.3 tools_3.1.2
编辑:我删除了一行不必要的代码,并添加了我的会话信息。