如何使用带有传单的控制层面板显示/隐藏图例?

hit*_*ton 3 r legend leaflet

我正在尝试绘制具有多个图层的地图。但是,当选择我想要在 controlLayer 面板中显示的组时,它会更改地图,但不会更改图例。事实上,这两个图例总是一起显示在地图上,但我一次只想要其中一个。

\n

这是我使用的代码:

\n
leaflet(data) %>%\n  addProviderTiles("Esri.WorldStreetMap") %>%\n  addRasterImage(r, opacity = 1, colors = cb, group = "Predictions") %>%\n  addLegend(pal = cb, values = wind_speed_range, title = "Wind Speed", opacity = 1, group = "Predictions", position = "topright") %>%\n  addCircles(radius = ~ residual*7000, color = "black", fillColor = ~ cb(predictions), fillOpacity = 1, label = ~ paste(as.character(residual), " / ", as.character(vitesse_vent), " / ", as.character(predictions)), weight = 2, group = "Predictions") %>%\n  addRasterImage(raster_difference, opacity = 1, colors = cb_correction, group = "Corrections") %>%\n  addLegend(pal = cb_correction, values = correction_range, title = "Corrections", opacity = 1, group = "Corrections", position = "topright") %>%\n  addLayersControl(\n    baseGroups = c("Pr\xc3\xa9dictions", "Corrections"),\n    options = layersControlOptions(collapsed = FALSE),\n    position = "topleft"\n  )\n
Run Code Online (Sandbox Code Playgroud)\n

正如你所看到的,我已经尝试过这篇文章的解决方案。我也尝试使用overlayGroup而不是baseGroup,但问题仍然存在。

\n

下图是我使用此代码得到的结果:\n在此输入图像描述

\n

你可以清楚地看到两个传说。

\n

tho*_*hal 7

代码

不幸的是,您没有提供代表,所以我用一个虚构的示例来展示它:

library(leaflet)

cities1 <- data.frame(City = factor(c("Boston", "Hartford", 
                                      "New York City", "Philadelphia", "Pittsburgh", "Providence")),
                      Lat = c(42.3601, 41.7627, 40.7127, 39.95, 40.4397, 41.8236),
                      Long = c(-71.0589, -72.6743, -74.0059, -75.1667, -79.9764, -71.4222), 
                      Pop = c(645966L, 125017L, 8406000L, 1553000L, 305841L, 177994L),
                      Type = factor(c("C", "D", "A", "A", "B", "C")))

cities2 <- data.frame(City = factor(c("Baltimore", "Ithaca", "Wareham")),
                      Lat = c(39.299236, 42.443962, 41.761452), 
                      Long = c(-76.609383, -76.501884, -70.719734), 
                      Pop = c(609032L, 30569L, 22666L),
                      Type = factor(letters[1:3]))




pal1 <- colorFactor("viridis", domain = cities1$Type)
pal2 <- colorFactor("Set1", domain = cities2$Type)

leaflet(cities1) %>% 
   addTiles() %>%
   addCircles(data = cities1, lng = ~Long, lat = ~Lat, weight = 1, group="one",
              radius = ~sqrt(Pop) * 30, popup = ~City, color = ~pal1(Type), opacity = .9
   ) %>%
   addLegend(pal = pal1, values = ~Type, group  = "one", layerId = "one") %>%
   addCircles(data = cities2, lng = ~Long, lat = ~Lat, weight = 1, group = "two",
              radius = ~sqrt(Pop) * 30, popup = ~City, color = ~pal2(Type), opacity = .9
              
   ) %>%
   addLegend(pal = pal2, values = ~Type, data = cities2, group = "two", layerId = "two") %>%
   addLayersControl(
      baseGroups = c("one", "two"),
      options = layersControlOptions(collapsed = FALSE),
      position = "topleft"
   ) %>% 
   htmlwidgets::onRender("
    function() { 
      var map = this;
      var legends = map.controls._controlsById;
      function addActualLegend() {
         var sel = $('.leaflet-control-layers-base').find('input[type=\"radio\"]:checked').siblings('span').text().trim();
         $.each(map.controls._controlsById, (nm) => map.removeControl(map.controls.get(nm)));
         map.addControl(legends[sel]);
      }
      $('.leaflet-control-layers-base').on('click', addActualLegend);
      addActualLegend();
   }")

Run Code Online (Sandbox Code Playgroud)

解释

您可以定义一些JavaScript对单选按钮的更改做出反应的自定义。当它们发生变化时,我基本上删除所有控件并添加所选控件。为了使其工作,我需要首先保存控件的副本。这当然有点 hackish(特别是因为我正在访问_controlsById地图的“私有”插槽),但从我对leafletAPI 的快速扫描来看,我没有找到更好的入口点。

截屏

动态图例