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..


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


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:

  • Thanks for the answer, I tried as you said. Did I do something wrong? Please see here Oh and I think your link is an API for Openlayers 2 or something..
    – Selphiron
    Mar 5 '15 at 0:03
  • 1
    well I looked for a similiar function in OL3 and there is! Please change your link to http://openlayers.org/en/master/apidoc/ol.layer.Layer.html#setVisible and the code to triangleLayer1.setVisible(value); so that I can accept the answer
    – Selphiron
    Mar 5 '15 at 9:39
  • Note also that you can set the visible property in the options object supplied when you create the layer - details can be found at the documentation link listed above
    – James Hill
    May 10 '16 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.