6 Apr 2018

Toggle Sheet Layer Visibility

Though we already have an article on this topic and things did not change that much, I thought it could still be useful to show the exact code that can be used now to toggle the visibility of a given layer on a sheet.

function toggleLayer(layerName, viewer) {
    var root = viewer.impl.getLayersRoot();

    if (root == null) {
        console.log("No layer information...");
        return;
    }

    var toggleLayerSub = function(layer, layerName, viewer) {
        if (layer.name === layerName) {
            var visible = !viewer.isLayerVisible(layer);
            viewer.setLayerVisible(
                [layer], // array of layers
                visible, // visible
                false    // isolate
            );
        }
    }

    for (var i = 0; i < root.childCount; i++) {
        var layer = root.children[i];

        // We can also check inside layer groups 
        if (!layer.isLayer) {
            for (var j = 0; j < layer.childCount; j++) {
                toggleLayerSub(layer.children[j], layerName, viewer);
            }
        } else {
            toggleLayerSub(layer, layerName, viewer);
        }  
    }
}

 

Related Article