1

How do I disable a particular overlay checkbox in the Openlayers Layerswitcher?

1 Answer 1

1

There is a layer property, displayInLayerSwitcher but it makes the entire layer dissaper instead of simply disableing the checkbox.

However, there are always work arounds:

DEMO

enter image description here

You can work that functionality into the layer class prototype like so:

OpenLayers.Layer.prototype.disableInSwitcher = function (disable) {

    if (disable) {

        if (!this.disabledInSwitcher) {
            var lSwitcher = this.map.getControlsByClass("OpenLayers.Control.LayerSwitcher")[0];

            for (var i in lSwitcher.dataLayers) {
                if (lSwitcher.dataLayers[i].layer.name == this.name) {
                    var attr = document.createAttribute("disabled");
                    attr.value = "disabled";
                    lSwitcher.dataLayers[i].inputElem.attributes.setNamedItem(attr);
                }
            }
            this.disabledInSwitcher = true;
        }
    } else {

        if (this.disabledInSwitcher) {
            var lSwitcher = this.map.getControlsByClass("OpenLayers.Control.LayerSwitcher")[0];

            for (var i in lSwitcher.dataLayers) {
                if (lSwitcher.dataLayers[i].layer.name == this.name) {
                    lSwitcher.dataLayers[i].inputElem.attributes.removeNamedItem("disabled");
                    console.log("here");
                }
            }
            this.disabledInSwitcher = false;
        }
    }
};

Then register the zoomend event like so:

map.events.register('zoomend', map, handleZoom);

And handle that event:

var handleZoom = function (event) {

    if (map.getZoom() > 2) {
        myFirstLayer.disableInSwitcher(true);
        mySecondLayer.disableInSwitcher(false);
    } else {
        myFirstLayer.disableInSwitcher(false);
        mySecondLayer.disableInSwitcher(true);
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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