Or automatically refresh/update the Legend based on what's user's layer selection on a specific group
If you build leaflet from Github, the addLegend function now accpets a group argument which ties the legend to that group and will auto show/hide as the ground is shown/hidden.
Changed the title to reflect the intended use case, also closing the issue as it's already supported in the trunk.
@bhaskarvk Is there any way to make this work with baseGroups? As of now, this seems to only be functional on overlay groups. Credit to this thread for the example.
library(raster)
library(leaflet)
#load in shapefiles
gtm <- getData('GADM', country = 'GTM', level = 0)
mex <- getData('GADM', country = 'MEX', level = 0)
leaflet() %>%
addTiles() %>%
addPolygons(data = gtm,
fillColor = 'red',
group = "gtm") %>%
addLegend(color = "red",
labels = gtm@data$GID_0,
group = "gtm") %>%
addPolygons(data = mex,
fillColor = 'blue',
group = "mex") %>%
addLegend(color = "blue",
labels = mex@data$GID_0,
group = "mex") %>%
addLayersControl(baseGroups = c("gtm", "mex"),
options = layersControlOptions(collapsed = F))
Most helpful comment
@bhaskarvk Is there any way to make this work with baseGroups? As of now, this seems to only be functional on overlay groups. Credit to this thread for the example.