Updating and removing graphics

The sections below explain how to:

  1. update a graphic, including its attributes, draw order, symbol, and geometry;
  2. remove graphics from a graphics layer.

Updating graphics in a graphics layer

There are various overloaded 'updateGraphic' methods on the GraphicsLayer class which allow for the following operations on an existing graphic:

In order for any of these operations to be carried out, the unique identification (ID) number of the graphic must me known. This can be obtained when the graphic is added to the graphics layer, or later on from a query or hit test on the graphics layer. The following code shows an example of how a point graphic can be moved to a new location:

// graphic ID stored, e.g. when adding the graphic
int graphicId = myGraphicsLayer.addGraphic(myGraphic);
//...
 
//create a point for the new location
Point newLocation = new Point(-290122, 7594445);
 
//apply that new point geometry to the graphic
myGraphicsLayer.updateGraphic(graphicId, newLocation);

Removing graphics from a graphics layer

Similar to when updating a graphic, the ID of the graphic is needed in order to remove it from a graphics layer. The exception to this is if you want to remove all the graphics from a graphics layer, in which case you can use the parameter-less method 'removeAll' on the GraphicsLayer instance.

The code snippet below shows how to remove a graphic from a graphics layer:

//add the graphic to the graphics layer
int graphicId = myGraphicsLayer.addGraphic(pointGraphic);
 
/* The GraphicsLayer class supports 2 remove methods, removeAll() and removeGraphic(int id).*/
 
//remove the graphic added above
myGraphicsLayer.removeGraphic(graphicId);
2/7/2013