Ewson Phang
Ewson Phang

Reputation: 43

How to add routes (polylines) between two markers in leaflet

I wish to add the polyline between these two point. How can I do this?

m <- leaflet() %>%
     addPolylines() %>%
     addTiles() %>%  # Add default OpenStreetMap map tiles
     addMarkers(lng = -87.6266382, lat = 41.8674336,
                popup = "starting") %>%
     addMarkers(lng = -87.64847, lat = 41.9168862,
                popup = "Destination")

m  # Print the map

Example of the data.

| region      | from_lat   | from_long    | to_lat      | to_long    |
|-------------|------------|------------- |-------------|----------- |
| 1           | 41.8674336 | -87.6266382  | 41.887544   | -87.626487 |
| 2           | 41.8674336 | -87.6266382  | 41.9168862  | -87.64847  |
| 3           | 41.8674336 | -87.6266382  | 41.8190937  | -87.6230967|

Upvotes: 4

Views: 5040

Answers (2)

SymbolixAU
SymbolixAU

Reputation: 26258

@jazzurro's answer is spot-on so that should remain the accepted answer.

As they alluded to, you can do this all within the googleway package if you want and plot a Google Map. The main difference to using leaflet is that you don't need to decode the polyline, the Google Map can handle the encoded string for you.

polylines <- lapply(1:nrow(mydf), function(x){

  foo <- google_directions(origin = unlist(mydf[x, 2:3]),
                           destination = unlist(mydf[x, 4:5]),
                           key = apiKey,
                           mode = "driving",
                           simplify = TRUE)

  ## no need to decode the line, just return the string as-is
  foo$routes$overview_polyline$points
}
)

df <- data.frame(polylines = unlist(polylines), stringsAsFactors = F)

## add some colour values for the markers
mydf$colour_from <- "red"
mydf$colour_to <- "blue"

## plot the polylines and the markers
google_map(key = mapKey) %>%
  add_markers(data = mydf, lat = "from_lat", lon = "from_long", colour = "colour_from") %>%
  add_markers(data = mydf, lat = "to_lat", lon = "to_long", colour = "colour_to") %>%
  add_polylines(data = df, polyline = "polylines")

enter image description here


Note: I'm the googleway author.

Upvotes: 2

jazzurro
jazzurro

Reputation: 23574

Here is my attempt. Since you want to draw some routes on a leaflet map, you want to achieve this task via the googleway package. It allows you to extract route data using google_directions() and decode_pl(). Since you have multiple routes, you want to use lapply() and create a data set. Once you have the route data, your job is straightforward; you use the data in addPolylines().

library(dplyr)
library(googleway)
library(leaflet)

mydf <- data.frame(region = 1:3,
                   from_lat = 41.8674336,
                   from_long = -87.6266382,
                   to_lat = c(41.887544, 41.9168862, 41.8190937),
                   to_long = c(-87.626487, -87.64847, -87.6230967))

mykey <- "you need to have your API key here"

lapply(1:nrow(mydf), function(x){

    foo <- google_directions(origin = unlist(mydf[x, 2:3]),
                             destination = unlist(mydf[x, 4:5]),
                             key = mykey,
                             mode = "driving",
                             simplify = TRUE)

    pl <- decode_pl(foo$routes$overview_polyline$points)

    return(pl)

        }
    ) %>%
bind_rows(.id = "region") -> temp


m <- leaflet() %>%
     addProviderTiles("OpenStreetMap.Mapnik") %>%
     addPolylines(data = temp, lng = ~lon, lat = ~lat, group = ~region) %>%
     addMarkers(lng = -87.6266382, lat = 41.8674336,
                popup = "starting")%>%
     addMarkers(data = mydf, lng = ~to_long, lat = ~to_lat,
                popup = "Destination")

enter image description here

Upvotes: 1

Related Questions