I'm trying to display WMS legends based on layer groups in Leaflet and Leaflet extras for R in Shiny. I am using input$map_groups as described here but it doesn't seem to work, any ideas on how to hide and toggle WMS legends?
Thanks,
Juan
library(shiny)
library(leaflet)
library(leaflet.extras)
# User Interface
ui <- bootstrapPage(
tags$style(type="text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width="100%", height="100%")
)
##### Shiny function server side
server = function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles("CartoDB.DarkMatter", options = tileOptions(minZoom = 0))%>%
addTiles(urlTemplate ="http://dataportal-dev.aquacross.eu/geoserver/gwc/service/tms/1.0.0/general:g2015_simplified@EPSG:900913@png/{z}/{x}/{y}.png",
options = tileOptions(noWrap = TRUE, tms = TRUE, opacity =0.9),group ="P1", layerId ="test")%>%
addTiles(urlTemplate ="http://dataportal-dev.aquacross.eu/geoserver/gwc/service/tms/1.0.0/general:country@EPSG:900913@png/{z}/{x}/{y}.png",
options = tileOptions(noWrap = TRUE, tms = TRUE, opacity =1),group ="P2", layerId ="test2")%>%
# addWMSLegend(position = "topright",uri='http://dataportal-dev.aquacross.eu/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=50&HEIGHT=20&LAYER=g2015_simplified', layerId ="test")%>%
addLayersControl(
baseGroups = c("P1", "P2"),
options = layersControlOptions(collapsed =FALSE)
)
})
## This is an attempt to show WMS legend maps based in groups
observeEvent(input$map_groups,{
map <- leafletProxy("map") %>% clearControls()
if (input$map_groups == 'P1')
{
map %>% addWMSLegend(position = "topright",uri='http://dataportal-dev.aquacross.eu/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=50&LAYER=g2015_simplified', layerId ="test")
}
else if (input$map_groups == 'P2')
{
map %>% addWMSLegend(position = "topright",uri='http://dataportal-dev.aquacross.eu/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=topp:states', layerId ="test2")
}
})
}
shinyApp(ui, server)