gpt4 book ai didi

javascript - R leaflet - 显示/隐藏带有组层的 addControl() 元素

转载 作者:可可西里 更新时间:2023-11-01 13:20:29 25 4
gpt4 key购买 nike

我有一张传单 map ,它使用 HTML 自定义图例并使用 addControl 函数添加(以下:Leaflet Legend for Custom Markers in R)。

但是,我只希望在显示一组时显示图例,我尝试使用参数 group = "group name",它不适用于 addControl功能。我也尝试过使用 layerId 参数但没有成功。

有什么想法吗?

可重现的例子:

library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]

# Choose Icon:
leafIcons <- icons(
iconUrl = ifelse(quakes$mag < 4.6,
"http://leafletjs.com/docs/images/leaf-green.png",
"http://leafletjs.com/docs/images/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94)

html_legend <- "<img src='http://leafletjs.com/docs/images/leaf-
green.png'>green<br/>
<img src='http://leafletjs.com/docs/images/leaf-red.png'>red"

# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons, group = "Group A") %>%
addMarkers(~long, ~lat, icon = leafIcons, group = "Group B") %>%
addControl(html = html_legend, position = "bottomleft") %>%
addLayersControl(position = "topleft", overlayGroups = c("Group A","Group B"))

我希望 addControl html_legend 元素仅在组 A 可见时可见。

最佳答案

你是想用它制作一个 Shiny-App 吗?我为 Siny-App 写了一些类似的东西,其中只显示点击组的图例。

如果它不应该是一个 Shiny 的应用程序,你可以做这样的事情(你必须将传单 map 分配给一个变量(在本例中为“ map ”)。所以你可以调用它并在之后调整它。

map <- leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons, group = "Group A") %>%
addMarkers(~long, ~lat, icon = leafIcons, group = "Group B") %>%
addControl(html = html_legend, position = "bottomleft") %>%
addLayersControl(position = "topleft", overlayGroups = c("Group A","Group B"))

groups <- map$x$calls[[5]]$args[[2]]
activeGroup <- map$x$calls[[3]]$args[[5]]

if (any(activeGroup %in% "Group A")) {
map %>% addLegend(title="Group A", position="bottomright", opacity=1, colors="red",
labels = "Group A")}

if (any(activeGroup %in% "Group B")) {
map %>% addLegend(title="Group B", position="bottomright", opacity=1,colors="green",
labels = "Group B")}

groups 变量存储所有当前的组,activeGroup 存储当前处于事件状态的组。然后,您可以将它与一些 if-else 语句一起使用,以仅显示事件组的图例。

尽管如此,它不会像普通 R 脚本那样具有交互性。您将不得不重复调用 activeGroup-call,以检查哪些组仍然处于事件状态。在 Shiny 中,将提供这种交互性。

这里是在 shiny-app 中的实现:

ui <- fluidPage(
leafletOutput("map")
)
server <- function(input, output, session){
output$map <- renderLeaflet({
map <- leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons, group = "Group A") %>%
addMarkers(~long, ~lat, icon = leafIcons, group = "Group B") %>%
addControl(html = html_legend, position = "bottomleft")
addLayersControl(position = "topleft", overlayGroups = c("Group A","Group B"))
map
})

observe({
map <- leafletProxy("map") %>% clearControls()

if (any(input$map_groups %in% "Group A")) {
map <- map %>% addLegend(title="Group A", position="bottomright", opacity=1, colors="red",labels = "Group A")}
if (any(input$map_groups %in% "Group B")) {
map <- map %>% addLegend(title="Group B", position="bottomright", opacity=1,colors="green",labels = "Group B")}
})
}

shinyApp(ui, server)

关于javascript - R leaflet - 显示/隐藏带有组层的 addControl() 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50373497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com