Adding temporary graphics

Some application functions add temporary graphics to the map of a map widget. For example, the Highlight feature action adds an animated graphic for the highlighted feature, and the Driving Directions map tool adds graphics to show the calculated route and the start and end points of the route.

Add-ins can also add temporary graphics to a map to show a geographical result of a feature action, tool, or widget function. This is an exception to the general rule of treating the Map of a MapWidget as read-only. See the Working with the Map topic for more information.

Recommendations for adding graphics to a map

Create a new ESRI.ArcGIS.Client.GraphicsLayer and add this to the AcceleratedDisplayLayers collection in the Map of the MapWidget. GraphicsLayers can be inserted in any order in the collection, and the exact order used may depend on the workflow of the operation view. Generally, points should be added on top of line graphics, and polygons added underneath lines. GraphicsLayers can also be inserted on top of the AcceleratedDisplayLayers collection

Do not add graphics to GraphicsLayers that already exist in the map; they may be used for other purposes and may be cleared or removed without warning. Use the ID property on the GraphicsLayer to uniquely identify the GraphicsLayer.

GraphicsLayers added at run time are not persisted as part of the map. They are discarded when the operation view is closed or when the MapWidget is removed or reconfigured with a different map.

How to add graphics to a map

Since the Xaml of the MapWidget is not available, GraphicsLayers must be added in code-behind. The following code shows how the AcceleratedDisplayLayers collection can be retrieved from the Map of a MapWidget, and a GraphicsLayer created and added to the collection, along with a single Graphic. The code also shows retrieving the GraphicsLayer from the collection by its ID. Symbols are set here in code but are typically specified as a Resource in an add-in.

Adding a GraphicsLayer C# example

// Create a GraphicsLayer and set ID.
client.GraphicsLayer tempGraphicsLayer = new client.GraphicsLayer()
{
  ID = uniqueString
};

client.Graphic tempGraphic = new client.Graphic()
{
  Geometry = mapW.Map.Extent.GetCenter(),
  Symbol = new client.Symbols.SimpleMarkerSymbol() { 
    Color = System.Windows.Media.Brushes.Red, Size = 12, Style = client.Symbols.SimpleMarkerSymbol.SimpleMarkerStyle.Circle}
};
tempGraphicsLayer.Graphics.Add(tempGraphic);

// Find the AcceleratedDisplayLayers collection from the Map and add the GraphicsLayer
client.AcceleratedDisplayLayers acLayers = mapW.Map.Layers.FirstOrDefault(lyr => lyr is client.AcceleratedDisplayLayers) as client.AcceleratedDisplayLayers;
acLayers.ChildLayers.Add(tempGraphicsLayer);

// Retrieve the GraphicsLayer if required using the ID
client.GraphicsLayer lyrOut = acLayers.ChildLayers[uniqueString] as client.GraphicsLayer;

Adding a GraphicsLayer Visual Basic example

' Create a GraphicsLayer and set ID.
Dim tempGraphicsLayer As client.GraphicsLayer = New client.GraphicsLayer() With {.ID = uniqueString}
Dim tempGraphic As client.Graphic = New client.Graphic() With { _
  .Geometry = mapW.Map.Extent.GetCenter(),
  .Symbol = New client.Symbols.SimpleMarkerSymbol() With { _
    .Color = System.Windows.Media.Brushes.Red,
    .Size = 12,
    .Style = client.Symbols.SimpleMarkerSymbol.SimpleMarkerStyle.Circle
  }
}
tempGraphicsLayer.Graphics.Add(tempGraphic)

' Find the AcceleratedDisplayLayers collection from the Map and add the GraphicsLayer
Dim acLayers As client.AcceleratedDisplayLayers = mapW.Map.Layers.FirstOrDefault(Function(lyr) TypeOf lyr Is client.AcceleratedDisplayLayers)
acLayers.ChildLayers.Add(tempGraphicsLayer)

' Retrieve the GraphicsLayer if required using the ID
Dim lyrOut As client.GraphicsLayer = DirectCast(acLayers.ChildLayers(uniqueString), client.GraphicsLayer)

For more information about adding graphics to a Map, see the How to add a graphics layer and How to add features to a graphics layer topics in the ArcGIS Runtime SDK for WPF help.

1/27/2015