Skip To Content

Manage graphic features

In this topic

Once you have a GraphicsLayer constructed, add graphics to it to display data. Graphics can be added programmatically or as a result of executing a query. The following is the basic workflow for adding graphics to a GraphicsLayer:

  • Create or retrieve a GraphicsLayer
  • Create or retrieve a graphic
  • Apply a symbol to the graphic
  • Add the graphic to the GraphicsLayer

Adding graphic features

Add the graphic to the GraphicsLayer.There are a number of ways to populate a GraphicsLayer with graphic features. Graphic features are immutable objects whose state cannot be modified once they are created. To create a graphic, you need a geometry that defines the graphic and a symbol with which to render the graphic. Optionally, you can define attributes to the graphic and/or an infotemplate containing the graphic's title template and content template.

The following code creates a graphic and adds it to an existing GraphicsLayer called gLayer:

// create a simple marker symbol to be used by our graphic
SimpleMarkerSymbol sms = new SimpleMarkerSymbol(Color.RED, 5, STYLE.CIRCLE));
// create a point geometry that defines the graphic
Point pnt = new Point(-1.2056463334580729E7, 4031300.7555036694);
// create the graphic using the symbol and point geometry
Graphic graphic = new Graphic(pnt, sms);
// add the graphic to a graphics layer
gLayer.addGraphic(graphic);

Graphic's draw order

You can add one or more graphics to a GraphicsLayer. Each graphic has a draw order associated with it that details what level the graphic should be drawn on your map with respect to other graphics in the graphics layer. As graphics get added to a graphics layer, the default draw order starts at 0. You can set the draw order when you construct the graphic object, by passing a draw order value. The draw order for the graphic can be obtained via a getter.

Graphic graphic = new Graphic(geometry, symbol, attributes, drawOrder);
int drawOrder = graphic.getDrawOrder();

You can bring a particular graphic to the top of a drawing order or set it to the bottom of the drawing hierarchy:

Bring graphic to front:

// get graphic ids from graphic layer
// assume 3 graphics in the graphics layer ordered 0, 1, 2
int[] graphicIDs = gLayer.getGraphicIDs();
// bring bottom graphic to top
gLayer.bringToFront(graphicIDs[2]);

Send graphic to back:

// get graphic ids from graphic layer
// assume 3 graphics in the graphics layer ordered 0, 1, 2
int[] graphicIDs = gLayer.getGraphicIDs();
// send top graphic to back
gLayer.sendToBack(graphicIDs[0]);