如何让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
相关产品推荐
相关产品推荐

