map.removeLayer(myLyr) does not work

9520
3
10-25-2010 09:40 AM
ShaningYu
Frequent Contributor
Scenario description:
Created several checkboxes.  If a checkbox is checked, a layer is added, or else, the layer will be removed (see the code below).  However, I can't remove the added layer(s).  I run debugging, it does go through map.removeLayer(lyr), but the layer I created is still on the map.  If you have a better approach or find anything wrong in my code, please help.  Thanks.

private function ChkSettings (chk:ImageCheckBox, lyr:GraphicsLayer,
    ac:ArrayCollection, lyrName:String ):void {
   if (chk.selected == false) {
lyr.clear();
for (var i:Number = 0; i < map.layers.length; i++) {
  if (map.layers.name == lyrName) {
   map.removeLayer(lyr);
  }
}
   } else {
lyr = AddGeocodeLayer2(ac);
lyr.name = lyrName;
map.addLayer(lyr);
   }
}
Tags (2)
0 Kudos
3 Replies
ShaningYu
Frequent Contributor
Problem solved by change
map.removeLayer(lyr);
to
map.removeLayer(map.layers);
0 Kudos
AdrianMarsden
Occasional Contributor III
Hi - I'm having issues with this too - I've tried

map.removeLayer(map.layers[0]);

(The layer I want to remove is index 0)

This results in[

CODE]



map.removeLayer('mainmap');

The gives no errors, but doesn't do anything, the layer is still showing if I Look at console.debug(map)

Cheers

ACM


0 Kudos
FabioPires
New Contributor II

Problem solved,

if (this.map.getLayer (layerID)) this.map.removeLayer (this.map.getLayer (layerID));

where layerID is the given layer.id when the layer is created, e.g., could be the same as lyr.name in your case.

0 Kudos