Skip to main content
Added reference to the OpenLayers bug, updated the answer to manage SelectControl with multiple layers
Source Link
Tommaso
  • 1k
  • 9
  • 13

Please update the question with a full example of code. The comment you recived should be enough:

  1. Call map.removeLayer() with a LAYER OBJECT: map.removeLayer(mLayers[a]
  2. Why are you calling layer.destroy()? If you destory a layer, you can't add it again to the map; removing it from the map should probably be enough for you

EDIT

The provided example works if the SelectControl is created with a Layer, not with an array of Layer:

var selectControl;
selectControl = new OpenLayers.Control.SelectFeature(
    Selected_Buildings,
    {
        clickout: true, toggle: false,
        multiple: false, hover: false
    }
 );
 map.addControl(selectControl);
 selectControl.activate();

Final answer

There could be a bug in OpenLayers.Layer.Vector.RootContainer, the class that is an an open bug with the SelectFeature control used to handleon multiple vectorlayers: the control creates a "root" layer with one control...I will check as soon as possible (thea copy of the rendered features, so when you remove a layer was "removed" from the map, but the features renderedare still visible on the map was not, this is whysince you still saware not removing the features on"hidden" root layer.

I didn't understand very well your use-case, but I think that there are 2 possible solutions:

  1. You can apply the patch proposed by the link above (it's easier to patch OpenLayers.debug.js, or you can build your own version of OL)
  2. You can use something like the following workaround (I understood that you have multiple kml layers on which you want the user to perform a select, I didn't understand if the user can remove only one of them, or all together....):

var pippo = new OpenLayers.Layer.Vector(...); // Layer1
var pluto = new OpenLayers.Layer.Vector(...); // Layer2
map.addLayers([pippo, pluto]);

// Creates the control on all the layers, activate and add it to the map 
selectControl = new OpenLayers.Control.SelectFeature([pippo, pluto]); 
map.addControl(selectControl);
selectControl.activate()

Then, to remove a layer, remove first the map)selectControl, then the layer

function removeLayer(layer) {

    // remove the control
    selectControl.deactivate();
    map.removeControl(selectControl);
    selectControl.destroy();

    // remove one layer
    map.removeLayer(pippo);

    // Re-create the control with the other layers or wathever
    selectControl = new OpenLayers.Control.SelectFeature(pluto);
    map.addControl(selectControl);
    selectControl.activate();

}

Please update the question with a full example of code. The comment you recived should be enough:

  1. Call map.removeLayer() with a LAYER OBJECT: map.removeLayer(mLayers[a]
  2. Why are you calling layer.destroy()? If you destory a layer, you can't add it again to the map; removing it from the map should probably be enough for you

EDIT

The provided example works if the SelectControl is created with a Layer, not with an array of Layer:

var selectControl;
selectControl = new OpenLayers.Control.SelectFeature(
    Selected_Buildings,
    {
        clickout: true, toggle: false,
        multiple: false, hover: false
    }
 );
 map.addControl(selectControl);
 selectControl.activate();

There could be a bug in OpenLayers.Layer.Vector.RootContainer, the class that is used to handle multiple vector layer with one control...I will check as soon as possible (the layer was "removed" from the map, but the features rendered on the map was not, this is why you still saw the features on the map)

The provided example works if the SelectControl is created with a Layer, not with an array of Layer:

var selectControl;
selectControl = new OpenLayers.Control.SelectFeature(
    Selected_Buildings,
    {
        clickout: true, toggle: false,
        multiple: false, hover: false
    }
 );
 map.addControl(selectControl);
 selectControl.activate();

Final answer

There is an an open bug with the SelectFeature control used on multiple layers: the control creates a "root" layer with a copy of the rendered features, so when you remove a layer from the map, the features are still visible on the map, since you are not removing the "hidden" root layer.

I didn't understand very well your use-case, but I think that there are 2 possible solutions:

  1. You can apply the patch proposed by the link above (it's easier to patch OpenLayers.debug.js, or you can build your own version of OL)
  2. You can use something like the following workaround (I understood that you have multiple kml layers on which you want the user to perform a select, I didn't understand if the user can remove only one of them, or all together....):

var pippo = new OpenLayers.Layer.Vector(...); // Layer1
var pluto = new OpenLayers.Layer.Vector(...); // Layer2
map.addLayers([pippo, pluto]);

// Creates the control on all the layers, activate and add it to the map 
selectControl = new OpenLayers.Control.SelectFeature([pippo, pluto]); 
map.addControl(selectControl);
selectControl.activate()

Then, to remove a layer, remove first the selectControl, then the layer

function removeLayer(layer) {

    // remove the control
    selectControl.deactivate();
    map.removeControl(selectControl);
    selectControl.destroy();

    // remove one layer
    map.removeLayer(pippo);

    // Re-create the control with the other layers or wathever
    selectControl = new OpenLayers.Control.SelectFeature(pluto);
    map.addControl(selectControl);
    selectControl.activate();

}
Edited afte the question has been improved with a working example
Source Link
Tommaso
  • 1k
  • 9
  • 13

Please update the question with a full example of code. The comment you recived should be enough:

  1. Call map.removeLayer() with a LAYER OBJECT: map.removeLayer(mLayers[a]
  2. Why are you calling layer.destroy()? If you destory a layer, you can't add it again to the map; removing it from the map should probably be enough for you

AgaiEDIT

The provided example works if the SelectControl is created with a Layer, improve your questionnot with an array of Layer:

var selectControl;
selectControl = new OpenLayers.Control.SelectFeature(
    Selected_Buildings,
    {
        clickout: true, toggle: false,
        multiple: false, hover: false
    }
 );
 map.addControl(selectControl);
 selectControl.activate();

There could be a full example!bug in OpenLayers.Layer.Vector.RootContainer, the class that is used to handle multiple vector layer with one control...I will check as soon as possible (the layer was "removed" from the map, but the features rendered on the map was not, this is why you still saw the features on the map)

Please update the question with a full example of code. The comment you recived should be enough:

  1. Call map.removeLayer() with a LAYER OBJECT: map.removeLayer(mLayers[a]
  2. Why are you calling layer.destroy()? If you destory a layer, you can't add it again to the map; removing it from the map should probably be enough for you

Agai, improve your question with a full example!

Please update the question with a full example of code. The comment you recived should be enough:

  1. Call map.removeLayer() with a LAYER OBJECT: map.removeLayer(mLayers[a]
  2. Why are you calling layer.destroy()? If you destory a layer, you can't add it again to the map; removing it from the map should probably be enough for you

EDIT

The provided example works if the SelectControl is created with a Layer, not with an array of Layer:

var selectControl;
selectControl = new OpenLayers.Control.SelectFeature(
    Selected_Buildings,
    {
        clickout: true, toggle: false,
        multiple: false, hover: false
    }
 );
 map.addControl(selectControl);
 selectControl.activate();

There could be a bug in OpenLayers.Layer.Vector.RootContainer, the class that is used to handle multiple vector layer with one control...I will check as soon as possible (the layer was "removed" from the map, but the features rendered on the map was not, this is why you still saw the features on the map)

Source Link
Tommaso
  • 1k
  • 9
  • 13

Please update the question with a full example of code. The comment you recived should be enough:

  1. Call map.removeLayer() with a LAYER OBJECT: map.removeLayer(mLayers[a]
  2. Why are you calling layer.destroy()? If you destory a layer, you can't add it again to the map; removing it from the map should probably be enough for you

Agai, improve your question with a full example!