R传单 - 单选按钮覆盖控制

时间:2018-01-24 09:43:07

标签: r leaflet r-leaflet

我的Leaflet地图上有7个叠加栅格图层,添加了addRasterImage。使用addLegend添加到图例时,会生成7个复选框。我可以检查和取消选中图层,但我想要的是一个单选按钮控件。选中新图层后,第一个图层将被取消选中。有没有人建议如何在R?

完成这项工作
    mymap.matteo <- leaflet(options = leafletOptions(minZoom = 3, maxZoom = 11)) %>%
  setView(lng = 50, lat = 42, zoom = 6) %>%

  addEasyButton(easyButton(
    icon='fa-globe', title='Pontocaspian extent',
    onClick=JS("function(btn, map){ map.setView([42,50],6); }"))) %>% # First lat then lon

  # Base groups
  addProviderTiles("Esri.WorldShadedRelief", group = "Shaded Relief") %>%
  addProviderTiles("Esri.WorldGrayCanvas", group = "Gray canvas") %>%

  # Overlay groups
  addRasterImage(Chl.variation, colors = palOranges(12), opacity = 1, group = "Chlorophyll variation") %>%
  addRasterImage(Dams, colors = palReds(12), opacity = 1, group = "Dams") %>%
  addRasterImage(Fishing, colors = palBlues(12), opacity = 1, group = "Fishing") %>%
  addRasterImage(Fuel.industry, colors = palBrowns(12), opacity = 1, group = "Fuel industry") %>%
  addRasterImage(Human.density, colors = palReds(12), opacity = 1, group = "Human density") %>%
  addRasterImage(Invasive.sp, colors = rev(viridis_pal(option = "D")(20)), opacity = 1, group = "Invasive species") %>%
  addRasterImage(Nutrient.load, colors = rev(viridis_pal(option = "A")(20)), opacity = 1, group = "Nutrient load") %>%
  addPolygons(data=PC.countries.shp, fill = FALSE, stroke = TRUE, color = "black", weight=1, opacity = 1, group = "Countries") %>%

  # Legend layers
  addLegend(pal = pal.chl.var, values = values(Chl.variation), position = "bottomright", labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)), title = "Chlorophyll variation", group = "Chlorophyll variation") %>%
  addLegend(pal = pal.dams, values = values(Dams), position = "bottomright", labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)), title = "Dams", group = "Dams") %>%
  addLegend(pal = pal.fishing, values = values(Fishing), position = "bottomright", labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)), title = "Fishing", group = "Fishing") %>%
  addLegend(pal = pal.fuel.industry, values = values(Fuel.industry), position = "bottomright", labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)), title = "Fuel industry", group = "Fuel industry") %>%
  addLegend(pal = pal.human.density, values = values(Human.density), position = "bottomright", labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)), title = "Human density", group = "Human density") %>%
  addLegend(pal = pal.invasive.species, values = values(Invasive.sp), position = "bottomright", labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)), title = "Invasive species", group = "Invasive species") %>%
  addLegend(pal = pal.nutrient.load, values = values(Nutrient.load), position = "bottomright", labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)), title = "Nutrient load", group = "Nutrient load") %>%

  # Layers control
  addLayersControl(
    position = c("topleft"),
    baseGroups = c("Gray canvas", "Shaded Relief"), 
    overlayGroups = c("Chlorophyll variation", "Dams", "Fishing", "Fuel industry", "Human density", "Invasive species", "Nutrient load", "Countries"),
    options = layersControlOptions(collapsed = F, autoZIndex = F)
  ) %>%

  hideGroup(c("Chlorophyll variation", "Dams", "Fishing", "Fuel industry", "Human density", "Invasive species", "Nutrient load"))

mymap.matteo

saveWidget(mymap.matteo, file="matteo.html")

1 个答案:

答案 0 :(得分:0)

baseGroups()提供了您要查找的内容:baseGroups()中的图层已通过单选按钮选中,并且一次显示一次。您必须将所有栅格放置在baseGroups()中。

还应该将底图移动到overlayGroups()上,否则当您选择带单选按钮的栅格时,底图将不会显示。您可能需要使用addMapPane()来确保底图出现在栅格的下面。