Googleway R 备选路线

Googleway R Alternative routes

我正在使用 google_directions() 函数生成两个坐标之间的行车路线,这两个坐标是通过点击传单中的不同点生成的 map.I 想展示一些可能的路线,而不是只有一个。设置 alternatives = TRUE 似乎不适合我。我试过多个坐标。

    key <- "my_key"
    #lat1,lon1 generated on first click. lat2,lon2 generated on second click.
    df <- google_directions(origin = c(lat1,lon1),
                            destination = c(lat2,lon2),
                            key = key,
                            mode = "driving",
                            simplify = TRUE,
                            alternatives = TRUE)
    pl <- decode_pl(direction_polyline(df))
    leaflet() %>% addTiles() %>% addPolylines(data = pl, lng = ~lon, lat = ~lat,group = "route")

我找到了 issue and have uploaded a fix

简而言之,正如您所发现的,alternatives 参数没有被正确使用。

如果您安装最新的开发版本,这应该适合您

devtools::install_github("SymbolixAU/googleway")

为了展示它的工作原理,这个例子将生成两条路线

library(googleway)

set_key("api_key")

df <- google_directions(origin = "Melbourne Airport, Australia",
                        destination = "Portsea, Melbourne, Australia",
                        mode = "driving",
                        alternatives = TRUE)


df_routes <- data.frame(polyline = direction_polyline(df)) 

set_key("map_key", api = "map")

google_map() %>%
  add_polylines(data = df_routes, polyline = "polyline")