Reputation: 39
I have 7 overlay raster layers on my Leaflet map added with addRasterImage. This results in 7 checkboxes when added to the legend using addLegend. I can check and uncheck layers, but what I would like is a radio button control. When a new layer is checked, the first layer get's unchecked. Has anybody suggestions on how to accomplish this in 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")
Upvotes: 1
Views: 2602
Reputation: 633
baseGroups()
offer what you are looking for: layers in baseGroups()
are selected with a radio button and are displayed one at the time. You have to place all your rasters in the baseGroups()
.
You should also move your basemaps to the overlayGroups()
, otherwise they will not show up when you select a raster with a radio button. You may have to play around with addMapPane()
to make sure the basemaps appear underneath the rasters.
Upvotes: 1