Remove XY graphics from map using flex list components.

764
5
02-03-2014 01:54 PM
KomanDiabate
New Contributor III
Hello All,
I am working on a project where I am using a list component to add XY locations to the map. The list I am using has allowMultipleSelection="true" and also has a click event to add the selectedItem XY value to map.
var myGraphicPoint:Graphic = new Graphic(new MapPoint(o.X, o.Y, new SpatialReference(3857)));

The problem I am trying to solve is the following when the user click on a selected item, the selectedItem is deselected but I am not able to remove the corresponding graphic from the map. I have tried several different things with no satisfactory results: for example, I have loop through the graphicsLayer and try to remove the graphics but it's not working. Does anybody knows how to do this or point me in the right direction.
Any helps will be appreciate.

Thanks
Tags (2)
0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus
Koman,

   It is had to tell what you are doing wrong without some code to check... Are you looping through the graphics in reverse order, i.e. -- not ++? You always need to loop in reverse order when you are removing items from an array that you are looping over.
0 Kudos
KomanDiabate
New Contributor III
Hi Robert,
To be honest, I have tried to loop the graphicProvider a while back and it didn't work so I went in a different direction, so I don't really have code to illustrate it but I can try to reproduce it again.
To answer your question, I am not using reverse looping even though I have seen an example of that somewhere while googling for answer.
This is the new direction I was looking at but that is not working also. Not sure how to approach this.
From you reply it sounds like I need to explore more the reverse looping idea... Here what I have so far, warning it rough and not working.

protected function nameList_changeHandler(event:IndexChangeEvent):void
{ 
 if(nameList.selectedIndex >= 0)
 {
  for(var i:int = 0; i < nameList.dataProvider.length; i++)
  {
   var o:Object = nameList.dataProvider.getItemAt(i);
   //trace(o.lastName);
   if (o.lastName==nameList.selectedItem.lastName)
   {
    var myGraphicPoint:Graphic = new Graphic(new MapPoint(o.X, o.Y, new SpatialReference(3857)));
    myGraphicPoint.symbol = BluDiamont;
    addressGL_Select.add(myGraphicPoint)      
   }
   else 
   {
    myGraphicPoint.symbol = sms1;
   }
   //nameList.dataProvider=null;
  }
 }  
}


Thanks a bunch for helping...
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Koman,

   There is not much for me to go off of there... I don't have enough time to just sit down and write an sample application for you, so the only advice I can give is that when you figure out your logic for determining if a graphic is to be removed from the Graphics Layer(GL) then you have to loop thought the GL in reverse order removing them:

for(var i:int = myGL.graphicProvider.length - 1; i >= 0; i--) {
    var gra:Graphic = myGL.graphicProvider as Graphic;
    //if some condition is true then remove the graphic from the GL
    if(gra.attributes.name == "some name"){
        myGL.remove(gra);
    }
}
0 Kudos
KomanDiabate
New Contributor III
Hi Robert,
The intent is not for you to write the code for me, I think I can do that myself. I just need a little bit of direction since I have been struggling with this for a while. I will use your code block as a new starting point and will ask more pointed questions if I need to. But I think your code block it will be very helpful.

Thanks a bunch.
0 Kudos
KomanDiabate
New Contributor III
FYI: I found another way to do this...

I think I was over thinking this one. So what I am doing here is to capture all the selected items in the list component into an arrayCollection and I then clear all the graphisc and re-add them. They are probably a better way to do this but, this is going to do it for me.

protected function nameList_changeHandler(event:IndexChangeEvent):void
{ 
 var selIndices:Vector.<int> = event.currentTarget.selectedIndices;
 var selItems:Vector.<Object> = event.currentTarget.selectedItems;
 var numItems:Number = selIndices.length;
 var myGraphicPoint:Graphic;
    
 //Clear GraphicsLayer
 addressGL_Select.clear();
 for (var ix:Number = 0; ix<numItems; ix++)
 {
  var cl:ArrayCollection=new ArrayCollection;
  cl.addItem(selItems[ix]);
     
  //CL and Add to Map
  for(var i:int = 0; i < cl.length; i++)
  {
   var o:Object = cl.getItemAt(i);
   myGraphicPoint = new Graphic(new MapPoint(o.X, o.Y, new SpatialReference(3857)));
   myGraphicPoint.symbol = BluDiamont;
   addressGL_Select.add(myGraphicPoint);
  }        
 } 
 selectList.addItem(nameList.selectedItem);
}
0 Kudos