如何解决如何从drawToolbar添加的传单地图中清除标记?
如何清除通过drawToolbar
的{{1}}地图上的leaflet
添加的标记?如果添加标记,然后单击shiny
,则标记仍保留在地图上。这是一个错误吗?我同时尝试了actionButton
和clearMarkers
,但是都没有。
clearGroup
解决方法
工具栏是开头创建的output$map
对象的一部分。要删除它,您需要为output$map
分配一个不包含工具栏的新值。例如,
library(shiny)
library(leaflet)
library(leaflet.extras)
ui <- fluidPage(
leafletOutput("map"),actionButton("rm","Remove markers")
)
server <- function(input,output,session) {
toolbar <- TRUE
init <- function() renderLeaflet({
leaf <- leaflet() %>% addTiles()
if (toolbar)
leaf <- leaf %>%
addDrawToolbar(targetGroup = "x")
leaf
})
output$map <- init()
observeEvent(input$rm,{
toolbar <<- !toolbar
output$map <- init()
leafletProxy('map') %>%
clearMarkers() %>%
clearGroup("x")
})
}
shinyApp(ui,server)
您应该可以调用removeDrawToolbar()
来摆脱它,但是该代码中有一个错误,其中列出了一些解决方法:https://github.com/bhaskarvk/leaflet.extras/issues/148
这是我想出的解决方案:
library(shiny)
library(leaflet)
library(leaflet.extras)
ui <- fluidPage(
leafletOutput("map"),session) {
init <- function() {
renderLeaflet({
leaf <-
leaflet() %>%
addTiles() %>%
addDrawToolbar()
bounds <- isolate({input$map_bounds})
if (!is.null(bounds)) {
center <- c("lat" = mean(c(bounds$north,bounds$south)),"lng" = mean(c(bounds$east,bounds$west)))
leaf <-
leaf %>%
setView(lng = center[["lng"]],lat = center[["lat"]],zoom = isolate({input$map_zoom}))
}
leaf
})
}
output$map <- init()
observeEvent(input$rm,{
output$map <- init()
})
}
shinyApp(ui,server)
它会在actionButton
单击时重绘地图(感谢@ user2554330的帮助)。如果存在不重绘地图的解决方案,例如使用clearMarkers()
函数,我会很感兴趣。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。