Calculate, decode and plot routes on map using leaflet and R

后端 未结 3 414
傲寒
傲寒 2021-01-03 00:43

I have raw data which consists of lattitude and longitude of places The sample data is as follows:

EDIT (dput):

structure(list(Lat = c(-33.9409444, -         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 01:09

    Another more efficient way to calculate routes between points is with the osrm package: Interface Between R and the OpenStreetMap-Based Routing Service OSRM. Look at this example:

    library(osrm)
    library(leaflet)
    
    df = data.frame(com = c("A", "B", "C"),
                    lon = c(31.043515, 31.029080, 31.002896),
                    lat = c(-29.778562, -29.795506, -29.836168),
                    time = as.POSIXct(c("2020-03-18 07:56:59","2020-03-18 12:28:58","2020-03-18 18:24:52")))
    
    
    trips <- osrmTrip(df, returnclass="sf")
    trip <- trips[[1]]$trip
    
    leaflet(trip) %>% 
      addProviderTiles("Stamen.TonerLite", group = "OSM") %>% 
      addPolylines() %>%
      addCircleMarkers(lat = df$lat,
                       lng = df$lon,
                       popup = paste(df$com,"-",format(df$time,"%H:%M:%S")),
                       color = "red",
                       stroke = FALSE,
                       radius = 8,
                       fillOpacity = 0.8)
    

提交回复
热议问题