使用 googleway
包中的 google_directions
函数,它不会根据 google map 浏览器搜索显示旅行时间。
好像没有考虑路况信息。
纽约繁忙街道上的示例。
a <- google_directions(origin = c(40.862804, -73.934743),
destination = "212 5th Ave, New York, NY 10010, USA",
mode = "driving",
units = "metric",
simplify = TRUE,
key = MY_API)
当前时间的响应:
b <- direction_steps(a)
total_time <- sum(b$duration$value)/60 # minutes
total_time # minutes
#[1] 26.1166
但是,对于相同的搜索时间,google map 浏览器显示 35 分钟。我检查了路线,它是一样的。
使用 mapsapi
包会发生同样的事情:
c <- mp_directions(origin = c(-73.934743,40.862804),
destination = "212 5th Ave, New York, NY 10010, USA",
mode = "driving",
key = MY_API)
total_time1 <- sum(mp_get_segments(c)$duration_s)/60 # minutes
total_time1 # minutes
# [1] 26.11667
有谁知道如何将交通信息整合到这些功能中?还是我应该断定谷歌不提供这种级别的信息?
最佳答案
google_directions
的 googleway
文档涵盖了这一点。要获取实时数据,您需要具备以下条件:
departure_time
,当前使用“现在”
或使用POSIXct
日期时间的指定 future 时间。traffic_model
:Google 的猜测应该是best_guess
、optimistic
还是pessimistic
。
您使用代码请求历史旅行时间,而 Google map 使用实时旅行时间(可能是 best_guess
估计值)。
The Google documentation states :
best_guess
(default) indicates that the returnedduration_in_traffic
should be the best estimate of travel time given what is known about both historical traffic conditions and live traffic. Live traffic becomes more important the closer thedeparture_time
is to now.
试试这个:
a <- google_directions(
origin = c(40.862804, -73.934743),
destination = "212 5th Ave, New York, NY 10010, USA",
mode = "driving",
units = "metric",
simplify = TRUE,
key = MY_API,
departure_time = "now",
traffic_model = "best_guess")
# Travel time in seconds
travel_time <- a$routes$legs[[1]]$duration_in_traffic$value
关于r - Googleway 不考虑旅行时间的交通(路线搜索),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651814/