Best Practices: Graphics layers, symbols, and renderers

Using the ArcGIS API for Microsoft Silverlight, you can dynamically display graphics on a map through a Graphics layer. A Graphics layer could, for example, be used to hold polygons drawn by a user or display features that satisfy a user-defined query.

Symbols define all the non-geographic aspects of a Graphic's appearance. This includes a Graphic's color, border width, transparency, and more. The ArcGIS API for Microsoft Silverlight includes many symbol classes, each of which allows you to specify symbology in a unique way. Each symbol type is also specific to one geometry type (that is, point, line, or polygon).

Rnderers define one or more Symbols to apply to a Graphics layer. The Symbol applied to each Graphic depends on the Graphic's attributes. The Renderer specifies which attribute values correspond to which Symbol.

The following sections detail some of the best practices for working with a GraphicsLayer, Symbols, and Renderers.

Set the GraphicsLayer.Renderer property

You must use the GraphicsLayer.Renderer property to set the layer’s symbology. If the Renderer property is not set when a GraphicsLayer is added to the map, the Map Web Part will automatically populate this property with a default renderer, therefore, you should set the GraphicsLayer.Renderer property before the layer is added to the map. A GraphicsLayer’s Renderer property always overrides any symbols set via Graphic.Symbol.

GraphicsLayer gl = new GraphicsLayer()
            {
                ID = "IdentifyResultsLayer",
                Renderer = new SimpleRenderer()
                {
                    Symbol = identifyDialog.Resources["RedMarkerSymbol"] as Symbol
                }
            };

Supported renderers

Only UniqueValueRenderer, ClassBreaksRenderer, and SimpleRenderer are currently supported for persistence and configuration. If an unsupported renderer type is used, users will not be able to configure the symbology of the results layer after it has been added to the map. As an example, consider a tool that adds a GraphicsLayer to the map. It is possible for users of the Application Builder to run such a tool while they are editing the Map Web Part. If they use a supported renderer type, users can configure the symbology of the results layer after it has been added to the map. Additionally, if they save or deploy that application, the results layer will be stored as part of that application’s map. These capabilities will not be available on GraphicsLayers that use renderer types that do not support persistence and configuration (for example, TemporalRenderer).

If a renderer type other than UniqueValueRenderer, ClassBreaksRenderer, or SimpleRenderer is used, do not allow the tool to be executed from within the Application Builder. This is because renderer types other than the ones listed do not support persistence and configuration. To do this, simply check whether Application.Current.IsEditMode is true, and, if so, prevent the tool from being run.

GraphicsLayer symbols

Declare the symbols used in your GraphicsLayer in a resource collection within your UI’s XAML. While symbols can be created programmatically, declaring them in XAML is more concise, readable, and generally easier to maintain.

<UserControl.Resources>
        <ResourceDictionary>
            <esri:SimpleMarkerSymbol x:Key="RedMarkerSymbol" Color="Red" Size="12" Style="Circle" />
        </ResourceDictionary>
    </UserControl.Resources>

Layer name

To retrieve or set the layer name that is shown in Map Contents panel, use MapApplication.LayerNameProperty. You can set the name by calling Layer.SetValue(MapApplication.LayerNameProperty, “layer name”), and retrieve the name by calling Layer.GetValue(MapApplication.LayerNameProperty).

GraphicsLayer gl = new GraphicsLayer();
 gl.SetValue(MapApplication.LayerNameProperty, "Identify Results");
gl.GetValue(MapApplication.LayerNameProperty);
8/12/2011