[GIS] toggle visibility of a Layer in Openlayers 3(!) with a Checkbox

htmljavascriptopenlayersopenlayers-2visibility

I have some vector layers on top of a tiled layer and a checkbox for each vector layer. I want a layer to be visible if its checkbox is checked and vanish if it gets unchecked.

I tried to call a function with the onchange attribute of the checkbox. It should set the visibility of the layer according to the (un)checked state of the checkbox.

This is my fiddle but unfortunately the checkboxes don't change anything.

edit: updated the code..

Best Answer

You need to call setVisibility(true/false) on the layer.

setVisibility

All you are doing in your code is changing the value of static variables. This will not trigger any action on the layer.

In your checkbox handler, you need to get a reference to the layer and change the visibility:

triangleLayer1.setVisible(value);