Search code examples
javascriptjqueryopenlayers

Simple OpenLayers function not working within jQuery function


I want to change the visibility (and some other things) of OpenLayers layers via buttons. When I call anyLayer.setVisibility("true") on the button click event via jQuery it is working:

$("#anyLayer_button").click(function(){
    anyLayer.setVisibility("true");
}

But when I want to do so for many layers using a superior jQuery function and thus calling the Ol layer with a variable firebug tells me "anyLayer.setVisibility is not a function".

function superiorFunction(anylayer) {
    $("#" + anyLayer + "_button").click(function(){
        anyLayer.setVisibility("true");
        otherFunction1(anyLayer);
        otherFunction2(anyLayer);
    });
}

The other (jQuery) functions are working within the function without any problems. How to solve this problem?


Solution

  • I found a solution:

    Instead of

    anyLayer.setVisibility("true");
    

    i call the layer with the getLayerByName function. It gives an array, from which i choose the first (and single) element:

    var layerArray = map.getLayersByName(layer);
    layerArray[0].setVisibility('true');