3

I'm working on a custom route planner in R at the moment. I'm using the output of the Google Maps Directions API. I want to show the route on a map between two places. Everything is going great so far. The only problem is that I don't know how to give the route multiple colors based on Speed at the moment. I searched the internet for a few days and I couldn't find something that fits my goal. That's why I made this post.

This is how my polyline data frame looks like:

Then I visualized it in Leafet with te following code:

#install.packages("leaflet")
library(leaflet)


pal <- colorNumeric(
palette = unique(polyline$Col),
domain = polyline$Speed,
na.color = "#FFFFFF"
)
rm(map)
map <- leaflet()
map <- addTiles(map)
a <- 1
for(a in length(unique(polyline$Step_ID))){


map <- addPolylines(map,lng = polyline$Lon, 
           lat = polyline$Lat,
           data = polyline[polyline$Step_ID==a,],
           color = polyline$col)
a <- a + 1
}
map <- addLegend(map,"bottomright", pal = pal, values = polyline$Speed,
        title = "Speed",
        opacity = 1)
map

So far I think you have to create multiple PolyLines(correct me if I'm wrong) to plot multiple colors in the route. That's why I made a for loop, to add ever PolyLine into the map.

This is how my visualization looks like

Everthing is just how want it. The only problem is the coloring of the line. I want the coloring of the lines just like Google does with traffic.

Can someone help me out with this please?

Uwe
  • 41,420
  • 11
  • 90
  • 134
Dave Meijdam
  • 33
  • 1
  • 4
  • 1
    can u `dput` your dataset – Sandipan Dey Feb 22 '17 at 12:16
  • .Label = c("red", "orange", "green"), class = "factor")), .Names = c("Step_ID", "Dist_Value", "Dist_Text", "Dur_Text", "Dur_Value", "Lat", "Lon", "Speed", "Col"), row.names = c(NA, 1448L), class = "data.frame") – Dave Meijdam Feb 22 '17 at 14:54

1 Answers1

3

To fully replicate your question you need to provide us with the actual data for polyline (i.e, not a screenshot). Until then, I'm going to create my own data set and show you how to create the coloured lines.

And, as you're using Google's API to get the directions, I'm assuming you'll have an API key, so I'm going to show you how to do it using my googleway package

library(googleway)

api_key <- "your_api_key"

directions <- google_directions(origin = "St Kilda, Melbourne, Australia",
                                destination = "Geelong, Victoria, Australia",
                                key = api_key)

## the results of the API give you distance in metres, and time in seconds
## so we need to calculate teh speed
spd <- (directions$routes$legs[[1]]$steps[[1]]$distance$value / 1000) / (directions$routes$legs[[1]]$steps[[1]]$duration$value/ 60 / 60)

## then we can start to build the object to use in the plot
## and as we are staying within Google's API, we can use the encoded polyline to plot the routes
## rather than extracting the coordinates
df <- data.frame(speed = spd,
                 polyline = directions$routes$legs[[1]]$steps[[1]]$polyline)



df$floorSpeed <- floor(df$speed)
colours <- seq(1, floor(max(df$speed)))
colours <- colorRampPalette(c("red", "yellow","green"))(length(colours))

df <- merge(df, 
            data.frame(speed = 1:length(colours), 
                       colour = colours), 
            by.x = "floorSpeed", 
            by.y = "speed")

map_key <- "your_map_api_key"

google_map(key = map_key) %>%
    add_polylines(data = df, polyline = "points", stroke_colour = "colour",
                   stroke_weight = 5, stroke_opacity = 0.9)

enter image description here

See this answer for a way of making the route planner in Shiny.

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • Oops sorry! I'm new here on the website. Thanks, this works for me! It only works on my personal Mac and not the Windows computer from my internship. What is a bit strange. But I'll figure out what the problem is. But thanks for your help and your package! – Dave Meijdam Feb 23 '17 at 08:47
  • @DaveMeijdam - is it an issue with installing the package on Windows, or viewing the map? – SymbolixAU Feb 23 '17 at 09:18
  • It doesn't give any errors when I'm running `devtools::install_github("SymbolixAU/googleway")`. But when I run the code of plotting the map, it doesn't show anything. The viewer stays blank. – Dave Meijdam Feb 23 '17 at 10:04
  • @DaveMeijdam - ah yes, I'm aware of this issue; I think it's within RStudio itself, but I haven't been able to track it down. If you go 'open in browser' it will show it in a browser window. – SymbolixAU Feb 23 '17 at 10:34
  • Ah I see, Thank you! You think it will work in shiny? – Dave Meijdam Feb 23 '17 at 10:45
  • @DaveMeijdam - yes (see my linked answer at the end of the post) - it should always work in a web browser, but you may see the same issue if you just use RStudio's native shiny browser – SymbolixAU Feb 23 '17 at 10:54
  • Thanks for the help! – Dave Meijdam Feb 23 '17 at 11:01
  • @DaveMeijdam - I should also mention, if you want the actual live traffic information, use the method `add_traffic()` – SymbolixAU Feb 23 '17 at 20:52