[GIS] How to deactivate Vector Selection In OpenLayers wms features

featuresgetfeatureinfoopenlayers-2vectorwms

This is my sample code and work fine :), But I have a little problem,

I'm using multiple layers (WMS), when I zoom the layer will be shown or hidden.
In my example bellow when OpenLayers level zoom is greater than 3 I'm showing layer2 and hiding layer1. And also I create a control to active highlight ability.
this example code for

        control = new OpenLayers.Control.GetFeature({
        protocol: OpenLayers.Protocol.WFS.fromWMSLayer(layer2),
         box: true,
         multipleKey: "shiftKey",
         toggleKey: "ctrlKey"
        });

        control.events.register("featureselected", this, function(e) {
              select.addFeatures([e.feature]);
        });
        control.events.register("featureunselected", this, function(e) {
              select.removeFeatures([e.feature]);
         });

        map.addControl(control);
        control.activate();

when OpenLayers level zoom less than 3 I'm showing back layer1 and hiding layer2. and I will deactivate highlighting control. and clearing all features selected.

I have trouble to do it, and bellow example code :

    // Layer 1
    layer1 = new OpenLayers.Layer.WMS(
        "Map A", serverUrl,
        {
            LAYERS: 'basic:layerA',
            STYLES: '',
            format: format
        },
        {
            isBaseLayer: true,
            visibility: true
        }
    );

    // Layer2
    layer2 = new OpenLayers.Layer.WMS(
        "Map B", serverUrl,
        {
            LAYERS: 'basic:layerB',
            STYLES: '',
            format: format,
            transparent: true,
            tiled: true,
        },
        {
            isBaseLayer: false,
            visibility: false,
            unsupportedBrowsers:[]
        }
    );

    select = new OpenLayers.Layer.Vector("Selection", {styleMap:
        new OpenLayers.Style(OpenLayers.Feature.Vector.style["select"])
    });

    map.addLayers([layer1,layer2,select]);

//Zoomend event 
    map.events.on({"zoomend": function (e) {

        console.log(this.getZoom());


    //I have trouble in here
        if (this.getZoom() > 3) {
            layer2.setVisibility(true);
            layer1.setVisibility(false);

            control = new OpenLayers.Control.GetFeature({
            protocol: OpenLayers.Protocol.WFS.fromWMSLayer(layer2),
             box: true,
             multipleKey: "shiftKey",
             toggleKey: "ctrlKey"
            });

            control.events.register("featureselected", this, function(e) {
                select.addFeatures([e.feature]);
            });
            control.events.register("featureunselected", this, function(e) {
                select.removeFeatures([e.feature]);
            });

            map.addControl(control);
            control.activate();

        }
        else{
            // In here, I will deactive controll above (hightligthing controll) 

            layer2.setVisibility(false);
            layer1.setVisibility(true);
        }
    }
    });

And should must I do ?

Best Answer

I believe you only need to call the event again but with unregister.

control.events.unRegister("featureunselected", this, function(e) {
                select.removeFeatures([e.feature]);
            });