Unable to clear graphic/limit user to one graphic shown

3397
1
11-15-2014 06:45 PM
ChrisMartin1
New Contributor

Hello,

I'm making an iOS app where the user clicks a point and a GP tool is run on the given X/Y of the point.  I've been successful in creating the point but each successive click adds another point.  I'd like the graphics layer to only contain the most recent point clicked.  The graphics flash off and back on after each click, so I think it is re-drawing.  Where am I going wrong?

Here is my code:

int graphicCount = 0;

-(void)addPoint:(AGSPoint*) mappoint{

   

    graphicCount+=1;

    AGSGraphicsLayer* myGraphicsLayer = [AGSGraphicsLayer graphicsLayer];

    [self.mapView addMapLayer:myGraphicsLayer withName:@"Graphics Layer"];

   

    //create a marker symbol to be used by our Graphic

    AGSSimpleMarkerSymbol *myMarkerSymbol =

    [AGSSimpleMarkerSymbol simpleMarkerSymbol];

    myMarkerSymbol.color = [UIColor blueColor];

   

    [myMarkerSymbol setSize:CGSizeMake(10,10)];

    [myMarkerSymbol setOutline:[AGSSimpleLineSymbol simpleLineSymbolWithColor:[UIColor redColor] width:1]];

   

    //Create an AGSPoint (which inherits from AGSGeometry) that

    //defines where the Graphic will be drawn

    AGSPoint* myMarkerPoint =

    [AGSPoint pointWithX:mappoint.x

                       y:mappoint.y

        spatialReference:[AGSSpatialReference wgs84SpatialReference]];

   

    //Create the Graphic, using the symbol and

    //geometry created earlier

    AGSGraphic* myGraphic =

    [AGSGraphic graphicWithGeometry:myMarkerPoint

                             symbol:myMarkerSymbol

                         attributes:nil];

   

    if(graphicCount>1){

        NSLog(@"DELETING OLD GRAPHICS");

        [myGraphicsLayer removeAllGraphics];

        graphicCount=0;

    }

   

    //Add the graphic to the Graphics layer

    [myGraphicsLayer addGraphic:myGraphic];

   

}

The if statement fires correctly but removeAllGraphics or removeGraphic: myGraphic do not remove the previous graphic.  Suggestions?  Thank you!

0 Kudos
1 Reply
GagandeepSingh
Occasional Contributor II

Hi Chris Martin‌,

In the addPoint method you are creating a new graphics layer every time. So every time you add a point its in a different graphics layer. Try creating a instance variable for the graphics layer and access the same variable in the addPoint method.

Good luck.

Gagan

0 Kudos