I have a leaflet map which has the draggable markers and shapes, user drawn features (with leaflet.draw plugin). Now I want to programmably retrieve all features in a layer/group from the leaflet map.
How should I achieve this function? Thanks for any suggestions.
Here I provided a simple example, a random set of markers is added into leaflet map with group markers
(codes directly copied from https://rstudio.github.io/leaflet/shiny.html and made some changes). My target here is to retrieve the position of all new markers.
My real question is for polygons and may be complicated than this, but method should be similar.
library(shiny)
library(leaflet)
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
ui <- fluidPage(
leafletOutput("mymap"),
p(),
actionButton("recalc", "New points"),
verbatimTextOutput('summary')
)
server <- function(input, output, session) {
points <- eventReactive(input$recalc, {
cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
}, ignoreNULL = FALSE)
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles("Stamen.TonerLite",
options = providerTileOptions(noWrap = TRUE)
) %>%
addMarkers(data = points(), group = 'markers')
})
output$summary <- renderPrint({
# Add some codes here
# .....
})
}
shinyApp(ui, server)
m$x$calls[[2]]$args[[1]]
because this just holds the parameters of theaddMarkers
function of your example. You'll probably need to write some javascript and useShiny.onInputChange
to send data back to theserver.R
. This post might be helpful to figure where to callShiny.onInputChange
. How are you using the draw plugin with Shiny?reactiveValues()
, which stores the parametes of the markers each time the object is drawn (on the button press). In your opinion is this what the OP is after?