trafficonese / leaflet.extras

Extra functionality for leaflet R package.
https://trafficonese.github.io/leaflet.extras/
GNU General Public License v3.0
211 stars 74 forks source link

Showing and hiding WMS legend when layer is turned on and off #214

Closed bmaparicio closed 2 months ago

bmaparicio commented 3 months ago

Hi, Is it possible to only show a WMS legend when the WMS layer is displaying? This is usually done by using setting a group in the legend, but group is not an argument of addWMSLegend.

Here is a reproducible example:

leaflet::leaflet()%>%
  leaflet::addTiles() %>%
  setView(73.917, 15.500 , zoom = 7)%>%
  leaflet::addWMSTiles("https://bhuvan-vec2.nrsc.gov.in/bhuvan/wms",layers = "GA_LULC50K_0506",
                       options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE),
                       group = "Exposure",
                       attribution = "")%>%
  leaflet.extras::addWMSLegend(uri="https://bhuvan-vec2.nrsc.gov.in:443/bhuvan/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=lulc%3AGA_LULC50K_0506",
                               layerId="Exposure")%>%
  leaflet::addLayersControl(overlayGroups = all_groups) %>%
  leaflet::hideGroup(all_groups)

image

trafficonese commented 2 months ago

Just add layerId = "Exposure" to your addWMSTiles.

The WMS-Tiles and the Legends are linked through their layerId