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

时间:2021-04-02 14:01:04

标签: r leaflet legend

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

这是我使用的代码:

leaflet(data) %>%
  addProviderTiles("Esri.WorldStreetMap") %>%
  addRasterImage(r, opacity = 1, colors = cb, group = "Predictions") %>%
  addLegend(pal = cb, values = wind_speed_range, title = "Wind Speed", opacity = 1, group = "Predictions", position = "topright") %>%
  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") %>%
  addRasterImage(raster_difference, opacity = 1, colors = cb_correction, group = "Corrections") %>%
  addLegend(pal = cb_correction, values = correction_range, title = "Corrections", opacity = 1, group = "Corrections", position = "topright") %>%
  addLayersControl(
    baseGroups = c("Prédictions", "Corrections"),
    options = layersControlOptions(collapsed = FALSE),
    position = "topleft"
  )

如您所见,我已经尝试过此 post 的解决方案。我也试过用overlayGroup代替baseGroup,但问题依旧。

下图是我使用此代码得到的: enter image description here

您可以清楚地看到两个图例。

1 个答案:

答案 0 :(得分:1)

代码

不幸的是,你没有提供 reprex,所以我用一个虚构的例子来展示它:

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();
   }")

说明

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

截图

Dynamic Legend

相关问题