rr-leafletgoogleway

Googleway R Alternative routes


I'm using the google_directions() function to generate driving routes between two coordinates which are generated on clicking at different points in a leaflet map.I would like to show a few possible routes, instead of just one. Setting alternatives = TRUE does not seem to do the job for me. I've tried multiple coordinates.

    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")

Solution

  • I found the issue and have uploaded a fix

    In brief, the alternatives argument wasn't correctly being used, as you found out.

    If you install the latest development version this should work for you

    devtools::install_github("SymbolixAU/googleway")
    

    To show it working, this example will generate two routes

    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")
    

    enter image description here