Manage Graphic Features
Once you have a GraphicsLayer constructed you will need to add Graphics to it in order 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
- Set the Graphics geometry type, if it has not been pre-defined.
- 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 which 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);
Graphics Draw Order
You can add one or more Graphics to a GraphicsLayer. Each graphic has a draw order associated with it which 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 id's 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 id's 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]);