I am using OpenLayers 3 and GeoServer to create a map with multiple layers. I am able to generate legend for each layer from GeoServer using GetLegendGraphic and display them using the following:
<div> <img src="http://localhost:8080/geoserver/wms?service=WMS&REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=12&HEIGHT=12&LAYER=country:Layer1"> </div>
I tried to control the legend visibility based on the visibility of its corresponding layer. I searched various sources and found How to add a legend and toggle the legend with a layer in Openlayers? which did not work for my case.
Now I have the following problem:
When a Map layer is made invisible, the legend is still displayed. How, can I control the legend visibility dynamic i.e. make the legend visible only when its corresponding layer is selected and make the legend invisible when its corresponding layer is invisible.
Is there a working example?