Graphics and Labels

381
2
11-04-2011 06:01 AM
Labels (1)
AndrewPurkis
New Contributor III
Is there a way of setting a label for a graphic?  I need to add a graphic layer containing vehicle locations using a MarkerSymbol, but I need a label displayed with the symbol, not by hovering the mouse over it.  I guess I could do it by adding a separate TextSymbol graphic, but it doesn't strike me as being a very efficient way of doing it.

Thanks

Andrew Purkis
0 Kudos
2 Replies
MichaelBranscomb
Esri Frequent Contributor
Hi,

To set labels for graphic features you can create your own custom MarkerSymbols (or LineSymbols/FillSymbols) by using a ControlTemplate. The following example defines a symbol in XAML in the Grid.Resources section then in code it creates 10 random points and assigns the custom symbol to them, labelling them with an attribute called "NAME".

XAML:

<Window x:Class="GraphicLabel.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="LayoutRoot">     
        <Grid.Resources>
            <esri:MarkerSymbol x:Key="labelSymbol" OffsetX="6" OffsetY="6">
                <esri:MarkerSymbol.ControlTemplate>
                    <ControlTemplate>
                        <Grid>
                            <!--Marker-->
                            <Ellipse Width="12" Height="12" Fill="Red" HorizontalAlignment="Left" VerticalAlignment="Top" />
                            <!--Label-->
                            <Grid Margin="8,8,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" IsHitTestVisible="False">
                                <!--Text halo using a white blurred text-->
                                <TextBlock Foreground="White" Text="{Binding Attributes[NAME]}" >
                                    <TextBlock.Effect>
                                        <BlurEffect Radius="5" />
                                    </TextBlock.Effect>
                                </TextBlock>
                                <!--Text-->
                                <TextBlock Foreground="Black" Text="{Binding Attributes[NAME]}" />
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </esri:MarkerSymbol.ControlTemplate>
            </esri:MarkerSymbol>
        </Grid.Resources>
        <esri:Map x:Name="_mapControl" Background="Gray" WrapAround="True">
            <esri:ArcGISTiledMapServiceLayer ID="arcGISTiledMapServiceLayer" 
                                             Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
            <esri:GraphicsLayer ID="graphicsLayer"/>
        </esri:Map>
    </Grid>
</Window>


CODE:

using System.Windows;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.Geometry;
using System;

namespace GraphicLabel
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Random random = new Random();
            GraphicsLayer graphicsLayer = _mapControl.Layers["graphicsLayer"] as GraphicsLayer;

            for(int i = 0; i <=10 ; i++)
            {
                Graphic graphic = new Graphic() 
                {
                Geometry = new MapPoint(random.Next(0,10000000),random.Next(0,10000000)),
                Symbol = LayoutRoot.Resources["labelSymbol"] as MarkerSymbol,
                };
                graphic.Attributes.Add("NAME",string.Format("Point {0}", i.ToString()));
                graphicsLayer.Graphics.Add(graphic);
            }
        }
    }
}


Cheers

Mike
0 Kudos
AndrewPurkis
New Contributor III
Hi Mike,

Exactly what I needed!

Thanks

Andrew
0 Kudos