r - Googleway 不考虑旅行时间的交通(路线搜索)

标签 r google-maps-api-3 google-directions-api googleway

使用 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 分钟。我检查了路线,它是一样的。

enter image description here

使用 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_directionsgoogleway 文档涵盖了这一点。要获取实时数据,您需要具备以下条件:

  • departure_time,当前使用 “现在” 或使用 POSIXct 日期时间的指定 future 时间。
  • traffic_model:Google 的猜测应该是best_guessoptimistic 还是pessimistic

您使用代码请求历史旅行时间,而 Google map 使用实时旅行时间(可能是 best_guess 估计值)。

The Google documentation states :

best_guess (default) indicates that the returned duration_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 the departure_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/

相关文章:

Android 谷歌地图 MapView 上的方向

r - ggrepel:结合使用 position_dodge 和 geom_label_repel

r - 将strptime函数应用于data.table的每个成员

R 报告者 : Editing Existing Slides

r - 将轴添加到 ggplots 的网格

google-maps - map 路由,类似于谷歌地图?

javascript - 更改谷歌地图中的javascript不会更新它?

javascript - 在 angularjs 函数中初始化谷歌地图 api

android - Google Directions API 是否考虑流量?

ios - 自动完成搜索 Swift 的 Google 说明