Flex 1.3 Toggle Graphic on and off

802
4
02-16-2011 07:31 AM
BryanWestphal
New Contributor
Im trying to toggle a graphic on and off based off a users click event.  I query the feature, but cannot find a way to check if the graphic already exists in the graphics layer to remove it, thus the graphics keep overlapping themselves.  Is there a way to check the graphics layer to see if that graphic already exists?

Thanks in advance.
Bryan
Tags (2)
0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus
Bryan,

   Just loop though the graphics in the graphicsProveder to see if one has the identical geometry.
0 Kudos
BryanWestphal
New Contributor
Robert,
Thanks for the response.
Here is what i have in a testing environment, but it seems that im not getting the geometry test to come back true.

private function onQueryExecuteComplete(event:QueryEvent):void
    {
var oneGraphic:Graphic = event.target as Graphic;
var fset:FeatureSet = event.featureSet;     
 
for each (var graphic:Graphic in fset.features)               
{      
  for each(var graLyrGraphic:Graphic in myGraphicsLayer.graphicProvider)
  {
   myInfo.text = "started loop";
   if (graLyrGraphic.geometry == graphic.geometry)
   {
    myInfo.text = "found similar geometry";
                                //THIS IS WHERE I WOULD REMOVE THE GRAPHIC
    return;
   }
  }
 
                graphic.symbol = new SimpleFillSymbol('solid',0xcc0000,.7)
  myGraphicsLayer.add(graphic);
  dist1PopTotal+=graphic.attributes.POPULATION;

}
return;
}
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Bryan,

   Try this instead.

   private function onQueryExecuteComplete(event:QueryEvent):void
   {
    var oneGraphic:Graphic = event.target as Graphic;
    var fset:FeatureSet = event.featureSet;
    
    for each (var graphic:Graphic in fset.features)
    {
     for each(var graLyrGraphic:Graphic in myGraphicsLayer.graphicProvider)
     {
      myInfo.text = "started loop";
      if (graLyrGraphic.attributes === graphic.attributes)
      {
       myInfo.text = "found similar geometry";
       //THIS IS WHERE I WOULD REMOVE THE GRAPHIC
       return;
      }
     }
     
     graphic.symbol = new SimpleFillSymbol('solid',0xcc0000,.7)
     myGraphicsLayer.add(graphic);
     dist1PopTotal+=graphic.attributes.POPULATION;
     
    }
    return;
   }
0 Kudos
BryanWestphal
New Contributor
Figured it out.  here is the code that works for me.  thanks again for your help robert

private function onQueryExecuteComplete(event:QueryEvent):void
  {
   var fset:FeatureSet = event.featureSet;    
   if(district1tool == true)
   {  
    for each(var graphic:Graphic in fset.features)                
    {       
      graphic.name = graphic.attributes.PRECINCT       
     for each(var graLyrGraphic:Graphic in myGraphicsLayer.graphicProvider)
     {
      if(graLyrGraphic.name == graphic.name)
      {
       for (var i:int = myGraphicsLayer.numGraphics - 1; i >= 0; i--)
       {
        if(myGraphicsLayer.graphicProvider.name == graphic.name)
        {
         if (district1tool == true)
         {
          dist1PopTotal -= myGraphicsLayer.graphicProvider.attributes.POPULATION
          myGraphicsLayer.remove(myGraphicsLayer.graphicProvider);
          return;
         }
        }
       }
       return;
      }
     }
     graphic.symbol = new SimpleFillSymbol('solid',0xcc0000,.7)
     myGraphicsLayer.add(graphic);
     dist1PopTotal+=graphic.attributes.POPULATION;
    }
    return;
   }
0 Kudos