Skip To Content ArcGIS for Developers Sign In Dashboard

Dynamic Layer Labeling

Download Samples Repository

Description

This sample demonstrates dynamic layer labeling.Labels are configured in the LayerDrawingOptions of the dyanmic layer.In this sample, labels are shown for major and minor US cities with different fonts and scale ranges.Major city label info is configured in the XAML while minor city label info is added via the code-behind.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.DynamicLayers.DynamicLayerLabeling"
             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/runtime/2013">
    <Grid>
		<esri:MapView x:Name="MyMapView" WrapAround="True">
            <esri:Map InitialViewpoint="-14675766.357,2695407.734,-6733121.861,6583994.101,102100">
                <esri:ArcGISDynamicMapServiceLayer x:Name="dynamicLayer" ID="USA"
				    ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer"
                    VisibleLayers="0,1,2">
                    
                    <!-- Major City Label Info -->
                    <esri:ArcGISDynamicMapServiceLayer.LayerDrawingOptions>
                        <esri:LayerDrawingOptionCollection>
                            <esri:LayerDrawingOptions LayerID="0" ShowLabels="True">
                                <esri:LayerDrawingOptions.LabelingInfos>
                                    <esri:DynamicLabelingInfo LabelExpression="[areaname]" LabelPlacement="PointAboveRight" Where="pop2000 > 500000">
                                        <esri:DynamicLabelingInfo.Symbol>
                                            <esri:TextSymbol Color="Blue">
                                                <esri:TextSymbol.Font>
                                                    <esri:SymbolFont FontFamily="Arial" FontSize="12" FontStyle="Italic" FontWeight="Bold" />
                                                </esri:TextSymbol.Font>
                                            </esri:TextSymbol>
                                        </esri:DynamicLabelingInfo.Symbol>
                                    </esri:DynamicLabelingInfo>
                                </esri:LayerDrawingOptions.LabelingInfos>
                            </esri:LayerDrawingOptions>
                        </esri:LayerDrawingOptionCollection>
                    </esri:ArcGISDynamicMapServiceLayer.LayerDrawingOptions>
                    
                </esri:ArcGISDynamicMapServiceLayer>
            </esri:Map>
        </esri:MapView>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media;

namespace ArcGISRuntime.Samples.Desktop.DynamicLayers
{
    /// <summary>
    /// This sample demonstrates dynamic layer labeling.  Labels are configured in the LayerDrawingOptions of the dyanmic layer.  In this sample, labels are shown for major and minor US cities with different fonts and scale ranges.  Major city label info is configured in the XAML while minor city label info is added via the code-behind.
    /// </summary>
    /// <title>Dynamic Layer Labeling</title>
	/// <category>Layers</category>
	/// <subcategory>Dynamic Service Layers</subcategory>
	public partial class DynamicLayerLabeling : UserControl
    {
        public DynamicLayerLabeling()
        {
            InitializeComponent();

			MyMapView.Map.SpatialReference = SpatialReferences.WebMercator;

            // Minor city label info
            DynamicLabelingInfo minorCityLabelInfo = new DynamicLabelingInfo();
            minorCityLabelInfo.LabelExpression = "[areaname]";
            minorCityLabelInfo.Symbol = new Esri.ArcGISRuntime.Symbology.TextSymbol()
            {
                Color = Colors.Black,
                Font = new SymbolFont("Arial", 10, SymbolFontStyle.Normal, SymbolTextDecoration.None, SymbolFontWeight.Normal)
            };
            minorCityLabelInfo.Where = "pop2000 <= 500000";
            minorCityLabelInfo.MaxScale = 0;
            minorCityLabelInfo.MinScale = 5000000;

            // Add minor city label info
            var labelInfos = dynamicLayer.LayerDrawingOptions.First(ldo => ldo.LayerID == 0).LabelingInfos;
            labelInfos.Add(minorCityLabelInfo);
        }
    }
}
Feedback on this topic?