2

I would like to show the distance between two locations using the leaflet package in a shiny app. I don't know how to do it. A line should connect two points on a map and display the distance on it. Please find the sample image: Path with distance

Any help on this would be greatly appreciated.

Eli Berkow
  • 2,628
  • 1
  • 12
  • 22
krishna
  • 401
  • 6
  • 16

1 Answers1

1

Try the below:

library(leaflet)
library(osrm)

route = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = 'full')
# route_simple = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = 'simplified')
route_summary = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = FALSE)

leaflet() %>% addTiles() %>% 
  addMarkers(c(115.6813467,150.3715249), c(-32.0397559,-33.8469759)) %>% 
  addPolylines(route$lon,route$lat, 
               label = paste(round(route_summary[1]/60), 'hr - ', round(route_summary[2]), 'km'), 
               labelOptions = labelOptions(noHide = TRUE))

leaflet() %>% addTiles() %>% 
  addMarkers(c(115.6813467,150.3715249), c(-32.0397559,-33.8469759)) %>% 
  addPolylines(route$lon,route$lat, 
               popup = paste(round(route_summary[1]/60), 'hr', br(), round(route_summary[2]), 'km'))

I added route_simple in the code above but didn't use it. It just saves less points on the route so it's less detailed but runs quicker. Feel free to play around. I also used a popup instead of a label in my second example. You need to click on the route to open it.

Label Example label example

Popup Example popup example

If you want a shiny app then see below:

library(leaflet)
library(osrm)

route = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = 'full')
# route_simple = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = 'simplified')
route_summary = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = FALSE)

library(shiny)

ui <- fluidPage(
  leafletOutput('map')
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>% addTiles() %>% 
      addMarkers(c(115.6813467,150.3715249), c(-32.0397559,-33.8469759)) %>% 
      addPolylines(route$lon,route$lat, 
                   label = paste(round(route_summary[1]/60), 'hr - ', round(route_summary[2]), 'km'), 
                   labelOptions = labelOptions(noHide = TRUE))
  })
}

shinyApp(ui, server)
Eli Berkow
  • 2,628
  • 1
  • 12
  • 22
  • 1
    Thanks a lot for this great help. I changed the coordinates of lat and lon, there I am getting error. I replaced these values c(54.77127, -18.99692), c(54.71064, -18.96314). "Error in gepaf::decodePolyline(res$routes$geometry): Wrong encoded string format". I am not able fix this issue. Could you please find out why it is giving such an error? – krishna Nov 24 '20 at 12:31
  • I am not sure as those coordinates work fine for me. Maybe make sure that all your packages are the latest versions. – Eli Berkow Nov 24 '20 at 12:50
  • Check your versions of gepaf, osrm and leaflet. – Eli Berkow Nov 24 '20 at 13:09
  • @ Eli Berkow: Great answer! Just a question - what is this br() function? Thank you so much! – stats_noob Feb 20 '22 at 18:02
  • br() is just a line break. Creates a new line. – Eli Berkow Feb 21 '22 at 15:14