Managing graphic features
Once you've created a graphics layer (as described in Creating a graphics layer), you need to add graphics to it to display data. In most cases, you'll create graphics using geometries that have been created by actions such as executing a query or drawing a shape on the map. To add a graphic, you need to do the following:
- Retrieve the GraphicsLayer where the graphic will be added.
- Create or retrieve a graphic.
- Set the graphic's geometry (if it's not already set).
- Apply the symbol to the graphic.
- Add the graphic to the GraphicsLayer.
Adding graphic features
While it's possible to add graphics to a map using XAML, you'll usually use .NET code (that is, code-behind). The following steps show how to add graphic features:
- Create an ArcGIS API for Windows Phone application with a Map and a GraphicsLayer. Your ContentPanel XAML should look as follows:
<Grid x:Name="ContentPanel" Grid.Row="1"> <esri:Map x:Name="MyMap" Extent="-120, 20, -100, 40"> <esri:Map.Layers> <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"> </esri:ArcGISTiledMapServiceLayer> <esri:GraphicsLayer ID="MyGraphicsLayer"/> </esri:Map.Layers> </esri:Map> </Grid>
Caution:Don't forget to add a reference to System.Runtime.Serialization in your project.
- Get a reference to the GraphicsLayer.
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
- In many cases, you'll get a reference to a graphic or a list of graphics by handling the events of objects such as tasks. In other instances, such as with draw surfaces, you'll create a graphic from a geometry. The code that follows assumes that you have already gotten a reference to a list of graphics and stored them in a variable called graphicsList. Define a loop to iterate through the graphics as follows:
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; foreach (Graphic graphic in graphicsList) { }
- Apply a symbol to each graphic. The following code assumes that a symbol with the key MySymbol has already been declared as a resource in the XAML. For information about creating symbols, see Symbols and renderers.
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; foreach (Graphic graphic in graphicsList) { graphic.Symbol = LayoutRoot.Resources["MySymbol"] as Symbol; }
Caution:If you have a renderer set on your GraphicsLayer, and the GraphicsLayer.RendererTakesPrecedence property is set to true (which is the default), the symbols set on individual graphics will be overwritten by the renderer. Set RendererTakesPrecendence to false to use the symbols set on individual graphics and only apply the renderer to the graphics without a symbol set.
- Once the symbol has been applied, add the graphic to the GraphicsLayer. This causes the graphic to be drawn on the map.
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; foreach (Graphic graphic in graphicsList) { graphic.Symbol = LayoutRoot.Resources["MySymbol"] as Symbol; graphicsLayer.Graphics.Add(graphic); }