Один из способов сделать это - предложить пользователю добавить текст после двойного щелчка на карте листовки.Двойной щелчок по координатам определяет, где разместить текст, а всплывающее окно - по тексту.
library(shiny)
library(leaflet)
library(leaflet.extras)
server = function(input,output,session){
# Create reactive boolean value that indicates a double-click on the leaflet widget
react_list <- reactiveValues(doubleClick = FALSE, lastClick = NA)
observeEvent(input$map_click$.nonce, {
react_list$doubleClick <- identical(react_list$lastClick, input$map_click[1:2])
react_list$lastClick <- input$map_click[1:2]
})
# Upon double-click, create pop-up prompt allowing user to enter text
observeEvent(input$map_click[1:2], {
if (react_list$doubleClick) {
shinyWidgets::inputSweetAlert(session, "addText", title = "Add text:")
}
})
# Upon entering the text, place the text on leaflet widget at the location of the double-click
observeEvent(input$addText, {
leafletProxy("map") %>%
addLabelOnlyMarkers(
input$map_click$lng, input$map_click$lat, label = input$addText,
labelOptions = labelOptions(noHide = TRUE, direction = "right", textOnly = TRUE,
textsize = "15px"))
})
# Clear out all text if user clears all layers via the toolbar
observeEvent(input$map_draw_deletestop, {
if ( length(input$map_draw_all_features$features) < 1 ) {
leafletProxy("map") %>% clearMarkers()
}
})
output$map <- renderLeaflet({
leaflet(options = leafletOptions(doubleClickZoom = FALSE)) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addMeasure(
primaryLengthUnit = "kilometers",
secondaryAreaUnit = FALSE) %>%
addDrawToolbar(
targetGroup ='draw',
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions()),
polylineOptions = filterNULL(list(shapeOptions = drawShapeOptions(lineJoin = "round", weight = 8))),
circleOptions = filterNULL(list(shapeOptions = drawShapeOptions(), repeatMode = F, showRadius = T,
metric = T, feet = F, nautic = F))) %>%
setView(lng = -73.97721, lat = 40.7640, zoom = 15)
})
}
shinyApp(ui = fluidPage( leafletOutput("map") ) , server)