r - 为什么我可以绕过我的防火墙以使用 ggmap 访问 Google 距离矩阵 API,而不是 R 中的 googleway 或 gmapsdistance?
问题描述
我正在尝试计算数千个起点和目的地的行驶时间和距离。googleway
但是,我公司的防火墙在使用or包时似乎引起了一些问题gmapsdistance
,但对于包来说工作得很好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
你可以看到这行得通!
但是,googleway
andgmapsdistance
包的等效功能似乎仍然存在绕过防火墙的问题。例如:
# 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_distance
都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
推荐阅读
- angular - 将数据绑定到 Angular 中的非本地 html 属性
- javascript - 将 {} 中的新对象添加到数组
- r - 使用 ggplot2 + gridExtra 进行无边界合并和调整绘图大小
- java - 将 SSL 证书安装到 Java 密钥库时出现问题
- python - 如何使用机器人框架在终端中执行命令?
- windows - 如何获取在 windows .bat 文件中捕获的命令中的所有参数
- angular - 如何增加 ag-grid 中自定义过滤器列表的宽度?
- php - 当我使用输入标签作为提交按钮时如何更改占位符
- node.js - 多端点 Graphql
- php - 从单个 CSV 条目导入多个自定义字段值