Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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);
    }
}
share|improve this answer
    
FYI: I noticed you have not accepted any answers to any of your questions. If you don't give credit to answers you'll have a low acceptance rate and people will be much less likely to answer your questions in the future. –  CaptDragon Sep 19 '12 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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