Skip To Content

Graphics layer

In this topic

Graphics layers allow you to dynamically display graphics on a map. A graphics layer could, for example, be used to hold polygons or lines drawn by a user, or display features that satisfy the results of a task such as query or geoprocessing. In the map below, a graphics layer is used to show the selected polygon of New Hampshire:

Graphics layer

The AGSGraphicsLayer class allows you to create a layer to hold your graphics. Instances of this class can contain a list of graphics, an optional renderer (AGSRenderer) as well as methods to add, remove, and redraw graphics. Add this layer after any base layers so that graphics display on top when your application runs.

Add a graphics layer

If you have not already done so, create an application with a map and create your graphics layer as follows:

AGSGraphicsLayer* myGraphicsLayer = [AGSGraphicsLayer graphicsLayer];

To add the new graphics layer to the map, use the AGSMapView method addMapLayer:

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

Add graphic features

A graphic is an instance of the AGSGraphic class. A graphic is associated with a geometry, which defines both the location and shape of the graphic on a map. The graphic can be associated with an indivdual symbol that defines how the graphic displays. Alternatively, the whole graphics layer can be associated with a renderer that decides how all graphics in that layer display. You can also have attributes associated with the graphic. These attributes are key-value pairs that contain information about the real-world entity that the graphic represents.

In most cases, the graphics you display on a map return as the result of executing tasks, such as a querying a map service, geocoding an address, or performing geoprocessing analysis. In such cases, each graphic contains a geometry that describes their location and shape on a map. Define how the graphics display, either by assigning each graphic a symbol or by assigning the graphics layer a renderer.

Alternatively, you may create indiviual graphics programmatically. In such cases, assign the graphic a geometry that you can define programmatically or let your user interactively sketch the geometry using the sketch layer.

The following code creates a graphic, assigns it a symbol and geometry, then adds it to an existing graphics layer:

//create a marker symbol to be used by our Graphic
AGSSimpleMarkerSymbol *myMarkerSymbol =
	[AGSSimpleMarkerSymbol simpleMarkerSymbol];
myMarkerSymbol.color = [NSColor blueColor];

//Create an AGSPoint (which inherits from AGSGeometry) that
//defines where the Graphic will be drawn
AGSPoint* myMarkerPoint =
	[AGSPoint pointWithX:-93.2984
		y:44.9409
		spatialReference:self.mapView.spatialReference];

//Create the Graphic, using the symbol and geometry created earlier
//A map automatically displays a callout whenever a 
//user clicks on a graphic and if that AGSGraphic object 
//has a valid infoTemplateDelegate. Set it to nil for 
//now.
AGSGraphic* myGraphic =
	[AGSGraphic graphicWithGeometry:myMarkerPoint
		symbol:myMarkerSymbol
		attributes:nil
  infoTemplateDelegate:nil];

//Add the graphic to the Graphics layer
[myGraphicsLayer addGraphic:myGraphic];

//Tell the layer to redraw itself
[myGraphicsLayer dataChanged];

Graphics returned as the result of a task are already populated with a geometry but not a symbol; therefore, give those graphics a symbol or apply a renderer to the graphics layer before adding them to your graphics layer. To set a symbol in an array of graphics returned as the result of a query task, do the following:

//create a simple fill symbol
AGSSimpleFillSymbol *fillSymbol =
	[AGSSimpleFillSymbol simpleFillSymbol];
fillSymbol.color =
	[[NSColor purpleColor] colorWithAlphaComponent:0.25];
fillSymbol.outline.color = [UIColor darkGrayColor];

//featureSet.features is the result of a Query task.
//It is an array of AGSGraphic objects containing
//geometries, but not symbols.
for (AGSGraphic *graphic in featureSet.features) {
	//set the graphics’s symbol to fillSymbol
	graphic.symbol = fillSymbol;

	//add the graphic to the layer
	[myGraphicsLayer addGraphic:graphic];

}
//Tell the layer to redraw itself
[myGraphicsLayer dataChanged];