问题
I am very excited the spatial abilities of Leaflet combined with R but I badly need the possibility to move around markers and/or draw paths over maps. As far as I see the Leaflet R package lacks this option albeit the original Java version could be forced this way. Do you have any idea?
回答1:
The question is quite high-level, but that being said, there is an answer here that provides a solution for drawing points on a map in a shiny app.
If you want to add lines between points and show the route traveled, use addPolylines()
. Example:
library(shiny)
library(dplyr)
library(leaflet)
travel <- data.frame("time" = c("6/20/17 13:32", "6/20/17 13:33", "6/20/17 13:34", "6/20/17 13:35", "6/20/17 13:36", "6/20/17 13:37"),
"lat" = c(59.313833, 59.312333, 59.309897, 59.307728, 59.300728, 59.298184),
"lon" = c(18.070431, 18.07431, 18.085347, 18.076543, 18.080761, 18.076176),
stringsAsFactors = F) %>%
mutate(
time = as.POSIXct(time, format = "%m/%d/%y %H:%M")
)
# define ui with slider and animation control for time
ui <- fluidPage(
sliderInput(inputId = "time", label = "Time", min = min(travel$time),
max = max(travel$time),
value = min(travel$time),
step=60, # set to increment by 60 seconds, adjust appropriately
animate=T),
leafletOutput("mymap")
)
server <- function(input, output, session) {
points <- reactive({
travel %>%
filter(time == input$time)
})
history <- reactive({
travel %>%
filter(time <= input$time)
})
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(lng = ~lon,
lat = ~lat,
data = points()) %>%
addMarkers(lng = ~lon,
lat = ~lat,
data = history()) %>%
addPolylines(lng = ~lon,
lat = ~lat,
data = history())
})
}
shinyApp(ui, server)
来源:https://stackoverflow.com/questions/41021237/animated-marker-moving-and-or-path-trajectory-with-leaflet-and-r