
Once you have created 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:
The discussion that follow will show you how to write code for each of these steps. All the .NET code shown is written in C#.
While it is possible to add Graphics to the Map using XAML, you will usually do this in .NET code (i.e. code-behind). But before you can add Graphics, you must get a reference to a Graphics layer. Assuming you are writing code in the code-behind of a page with XAML specified as shown above, you would retrieve a GraphicsLayer as follows:
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 called MySymbol has already been declared. For information about creating symbols refer to the document Working with Symbols and Renderers.
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
foreach (Graphic graphic in graphicsList)
{
graphic.Symbol = MySymbol;
}Once the Symbol has been applied, add the Graphic to the Graphics layer. 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 = MySymbol;
graphicsLayer.Graphics.Add(graphic);
}