【问题标题】:Failure to geocode using the ggmap and Google Geocoding API in R; behind company firewall未能在 R 中使用 ggmap 和 Google Geocoding API 进行地理编码;在公司防火墙后面
【发布时间】:2019-08-31 09:26:09
【问题描述】:

我正在尝试使用ggmap 包中的geocode() 函数对R 中的地址进行地理编码。我在我的个人计算机上相对容易地完成了这个,并想在我的工作计算机上尝试这个。一般来说,我应该在谷歌注册,库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/端口,因为我想这是特定于我的计算机的。

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

【问题讨论】:

    标签: r geocoding google-geocoder ggmap google-geocoding-api


    【解决方案1】:

    我在工作中遇到了类似的问题,我设法用httr 库中的函数添加了一行代码。

    我做到了:

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

    只需插入贵公司网络中的计算机连接到互联网的代理和需要打开的端口。常用的 Web 代理服务器端口有 3128、8080、6588 和 80。

    希望这会有所帮助!

    【讨论】:

    • 感谢您的帮助。不幸的是,我仍然收到一条错误消息:“curl::curl_fetch_memory(url, handle = handle) 中的错误:无法连接到 端口 8080:连接被拒绝”。我尝试了每个端口。端口 80 产生不同的错误:CONNECT 后从代理收到 HTTP 代码 405。我相信我使用了正确的网址(在 IE 中,工具 > Internet 选项 > 连接选项卡 > LAN 设置)。它目前处于自动配置中,选中了“使用自动配置脚本”。我什至用同样的错误插入了我认为是我的官方用户名和密码。
    • 谢谢。最终你的解决方案奏效了,但我获得代理和端口的方式是错误的。请参阅我的答案以获得进一步的解释。再次感谢!
    • 很高兴它最终为你解决了!我不知道curl 包提供了获取代理和端口的选项,很有用。 :)
    【解决方案2】:

    在尝试了here 的每个解决方案后,都没有工作,我通过反复试验找出了问题所在。最终,我的代理和端口是错误的。在我的示例中,我按照链接中的说明通过 IE -> 工具 -> Internet 选项 -> 连接选项卡 -> LAN 设置找到我的代理。但是,代理与我的计算机使用的有些不同。因此,一个万无一失的方法是使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 2011-08-09
      • 2013-08-29
      • 2019-06-23
      • 2017-02-27
      • 1970-01-01
      相关资源
      最近更新 更多