簡體   English   中英

為什么我可以繞過防火牆來使用 ggmap 訪問 Google Distance Matrix API,而不是 R 中的 googleway 或 gmapsdistance?

[英]Why can I bypass my firewall to access Google Distance Matrix API with ggmap, but not googleway or gmapsdistance in R?

我正在嘗試計算數千個起點和目的地的行駛時間和距離。 但是,我公司的防火牆在使用googlewaygmapsdistance包時似乎會導致一些問題,但對於ggmap包運行良好。 例如:

# Library packages
library(ggmap)
library(googleway)
library(gmapsdistance)

# Set my origin and destination places
org   <- "waco, texas"
dest  <- "houston, texas"

# Register my key (note: insert your own Google key)
myKey <- "insert_key"
register_google(key = myKey)

# Calculate distance
mapdist(from = org, to = dest, mode = "driving")

# Source : https://maps.googleapis.com/maps/api/distancematrix/json?
# origins=waco,+texas&destinations=houston,+texas&key=xxx&mode=driving
# Error in curl::curl_fetch_memory(url, handle = handle) : 
#  Timeout was reached: Connection timed out after 10000 milliseconds

通過使用其他問題中所述的解決方案和使用curl包,我已經能夠繞過這個問題,例如:

library(curl)

# Get ie proxy
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)

# Register my key
register_google(key = myKey)

# Calculate distance
mapdist(from = org, to = dest, mode = "driving")

# Source : https://maps.googleapis.com/maps/api/distancematrix/json?# origins=waco,+texas&destinations=houston,+texas&key=xxx&mode=driving
# A tibble: 1 x 9
#  from        to                  m    km miles seconds minutes hours mode   
#  <chr>       <chr>           <int> <dbl> <dbl>   <int>   <dbl> <dbl> <chr>  
# 1 waco, texas houston, texas 297148  297.  185.   10235    171.  2.84 driving

你可以看到這是有效的!

但是, googlewaygmapsdistance包的等效功能似乎仍然存在繞過防火牆的問題。 例如:


# Using googleway
google_distance(org, dest, mode = "driving", key = myKey)

# Error in value[[3L]](cond) : 
#  There was an error downloading results. Please manually check the following # URL is valid by entering it into a browswer. If valid, please file a bug 
# report citing this URL (note: your API key has been removed, so you will need # to add that back in) 

# https://maps.googleapis.com/maps/api/distancematrix/json?# 
#&origins=waco,+texas&destinations=houston,+texas&alternatives=false&units=metri# c&mode=driving&key=

# Using gmapsdistance
gmapsdistance(origin = "Washington+DC", destination = "Chicago+IL", mode = "driving", key = myKey)

#Error in function (type, msg, asError = TRUE)  : 
#  Failed to connect to maps.googleapis.com port 80: Timed out

您可以看到google_distancegmapsdistance都失敗,似乎是防火牆錯誤。

有人可以幫助我了解如何始終繞過防火牆,以及如何使用其他兩個包來計算行駛距離和時間嗎?

根據與Google Trends的比較,我們發現 ggmap 的搜索次數明顯多於其他兩個庫。

最有可能的情況是您的公司已經將 ggmap(但不是其他 googleway 或 gmapsdistance)的源引用列入白名單,因為它相對受歡迎。

在 Renviron.site 中設置 http 代理適用於 gmapsdistance。

例子:

http_proxy=http://proxy.dom.com/
http_proxy_user=user:passwd

https_proxy=https://proxy.dom.com/
https_proxy_user=user:passwd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM