Please update the question with a full example of code. The comment you recived should be enough:
- Call map.removeLayer() with a LAYER OBJECT: map.removeLayer(mLayers[a]
- 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:
- 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)
- 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();
}