You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让R的googleway包google_directions按距离优化路线?

按距离优化google_directions路线的方法

要实现按距离优化途径点的需求,你需要结合Google Maps Directions API的参数和googleway包的google_directions函数配置:

  • 调用google_directions时,除了设置optimise_waypoints = TRUE,额外添加route_preference = "DISTANCE_OPTIMAL"参数,这会强制API以总距离最短作为路线优化的核心依据,替代默认的时间优先逻辑。

示例代码:

library(googleway)

# 替换为你的Google Maps API密钥
api_key <- "YOUR_API_KEY"

# 定义起点、途径点、终点
origin <- "Beijing, China"
waypoints <- c("Tianjin, China", "Langfang, China")
destination <- "Shijiazhuang, China"

# 发起按距离优化的路线请求
directions_result <- google_directions(
  origin = origin,
  waypoints = waypoints,
  destination = destination,
  key = api_key,
  optimise_waypoints = TRUE,
  route_preference = "DISTANCE_OPTIMAL"  # 关键参数:指定距离优先
)

补充说明

Google Maps Directions API的route_preference参数有两个可选值:

  • "TRAFFIC_AWARE_OPTIMAL":默认值,优先结合实时交通计算最短行程时间
  • "DISTANCE_OPTIMAL":优先计算总距离最短的路线,忽略实时交通对时间的影响

该参数仅适用于驾车模式(mode = "driving",为google_directions默认模式),步行、骑行等出行模式不支持此配置。

内容的提问来源于stack exchange,提问作者DatamineR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 18:52:11