Select to view content in your preferred language

Removing Graphics from Layer

1440
2
12-05-2010 07:12 PM
SimChin_Siang
New Contributor
Hi folks,

I encountered problems removing Graphic from GraphicsLayer.

My code is as below,

if(themesArray.removeItem(tName))
{
   check = true;
}
else
{
  themesArray.addItem(tName);
}
         
for(var i:int=1; i < response.SrchResults.length; i++)
{
  var theme:Object = response.SrchResults;
    
  var str:String = theme.XY;
  var subStrIndex:int;
  var x:String;
  var y:String;
    
  subStrIndex = str.search(",");
         

x = str.substr(0, subStrIndex);
y = str.substr(subStrIndex + 1);
     
var mp:MapPoint = new MapPoint(parseFloat(x), parseFloat(y), new SpatialReference(102100));
     
var myGraphicMarker:Graphic = new Graphic(mp,
new PictureMarkerSymbol("http://www.onemap.sg/icons/"+ tName + "/" + theme.ICON_NAME, 25, 25));
    
myGraphicMarker.toolTip = theme.NAME;
if(check == true)
{      
  gLayer.remove(myGraphicMarker);
}
else
{
  gLayer.add(myGraphicMarker);
}
}
}

What i want to do is to remove graphics that is already on the map if previously had already added.
For example first time i selected something new, it will be added to the array and it will created new graphic to add to the Layer.

But if the graphics has been added previously, i want to remove it from the map instead.

Seems like gLayer.remove(myGraphicMarker); doesn't work.

Any experts can render some help?

Thanks in advanced!
Tags (2)
0 Kudos
2 Replies
DasaPaddock
Esri Regular Contributor
gLayer.remove(myGraphicMarker) requires the passed in Graphic to be the same instance as a Graphic already inside the GraphicsLayer otherwise it doesn't know which one to remove. It looks like you're creating a new instance which won't work. You could instead loop through the graphicsProvider to find the instance you want to remove and then pass that to gLayer.remove().
0 Kudos
SimChin_Siang
New Contributor
Thank you Dasa Paddock for the help.
0 Kudos