无法使用R中的ggmap和Google Geocoding API进行地理编码;公司防火墙背后

问题描述 投票:0回答:2

我试图使用R包中的geocode()函数对ggmap中的地址进行地理编码。我已经在我的个人计算机上相对容易地做到了这一点,并希望在我的工作计算机上尝试这个一般来说,我应该注册Google,库ggmap包,读入我的安全密钥,然后我可以使用geocode()函数。但我得到错误。见下文:

# Library package
library(ggmap)

# Set key file
gmAPI     <- "key_file.txt"

# Read-in Google API key
gmAPIKey  <- readLines(gmAPI)[1]

# Register key
register_google(key = gmAPIKey)

# Geocode Waco, TX
geocode("waco, texas", output = "latlona")

我没有生成地理编码输出,而是收到:

Source : https://maps.googleapis.com/maps/api/geocode/json?address=waco,+texas&key=xxx.txt
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Failed to connect to maps.googleapis.com port 443: Timed out

或有时:

Source : https://maps.googleapis.com/maps/api/geocode/json?address=waco,+texas&key=xxx.txt
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Failed to connect to url port ###: Connection refused

注意:我用url port ###替换了错误消息中发布的实际url / port,因为我想这是特定于我的计算机的。

我觉得这与我的工作网络有关。 Similar questions使用httr包设置了一些配置,但这些解决方案对我没用。我可能输入了错误的信息。有帮助吗?

r geocoding google-geocoder ggmap google-geocoding-api
2个回答
1
投票

我的工作中遇到了类似的问题,我设法解决了从httr库添加一行代码的问题。

我做了:

library(httr)
set_config(use_proxy(url="http://proxy.mycompanyname.com", port=****))

只需插入代理,通过该代理,公司网络中的计算机将连接到Internet和需要打开的端口。常用的Web代理服务器端口是3128,8080,6588和80。

希望这可以帮助!


1
投票

在尝试了每个解决方案here,并且没有一个工作之后,我通过反复试验找出了问题。最终,我的代理和端口是错误的。在我的示例中,我按照链接中的说明通过IE - >工具 - > Internet选项 - >连接选项卡 - >局域网设置查找我的代理。但是,代理与我的计算机使用的有些不同。因此,一种简单的方法是使用curl包并使用ie_get_proxy_for_url()函数以编程方式执行此操作。当我使用ie_get_proxy_for_url()函数的输出时,@ Lennyy的解决方案起作用(因此被记入)。看代码:

library(curl)
library(ggmap)
library(httr)

# Get proxy and port
proxyPort <- ie_get_proxy_for_url()

# Split the string to feed the proxy and port arguments
proxyURL  <- strsplit(proxyPort, ":")[[1]][1]
portUsed  <- as.integer(strsplit(proxyPort, ":")[[1]][2])

# Set configuration
set_config(use_proxy(url=proxyURL, port = portUsed), override = TRUE)

# Geocode Waco, TX
geocode("waco, texas", output = "latlona")

# Output commented below:
# A tibble: 1 x 3
#    lon   lat address      
#  <dbl> <dbl> <chr>        
# 1 -97.1  31.5 waco, tx, usa
© www.soinside.com 2019 - 2024. All rights reserved.