Managing graphic features
Once you have created a graphics layer (discussed in Creating a graphics layer), you need to add graphics to it in order to display data. In most cases, you will 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 must:
- Retrieve the GraphicsLayer to add the graphic to
- Create or retrieve a graphic
- Set the graphic's Geometry, if this hasn't been done for you
- Apply the Symbol to the graphic
- Add the graphic to the GraphicsLayer
Adding graphic features
While it is possible to add graphics to the Map using XAML, you will usually do this in .NET code (i.e. 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 as described in the topic Creating a graphics layer. Your ContentGrid XAML should look as follows:
<Grid x:Name="ContentGrid" 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 will get a reference to a graphic or a list of graphics by handling the events of objects like tasks. In other instances, such as with draw surfaces, you will 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. First, define a loop to iterate through the graphics:
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; foreach (Graphic graphic in graphicsList) { }
- Apply a symbol to each graphic. This 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 Working with symbols and renderers.
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; foreach (Graphic graphic in graphicsList) { graphic.Symbol = LayoutRoot.Resources["MySymbol"] as Symbol; }
- Once the symbol has been applied, add the graphic to the GraphicsLayer. This will cause 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); }
12/1/2010