Label Point

Download Samples Repository

Description

This sample demonstrates use of the GeometryEngine.LabelPoint method to calculate the location of label points.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.LabelPoint"
             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 x:Name="layoutGrid">
        <Grid.Resources>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

            <esri:PictureMarkerSymbol x:Key="PictureMarkerSymbol" XOffset="12" YOffset="12" />
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-15053000,2749000,-6540000,6590000">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
			</esri:Map>
			
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="labelGraphicOverlay">
					<esri:GraphicsOverlay.Renderer>
						<esri:SimpleRenderer>
							<esri:SimpleFillSymbol Color="#7F0000FF"    >
								<esri:SimpleFillSymbol.Outline>
									<esri:SimpleLineSymbol Color="Blue" Width="2"/>
								</esri:SimpleFillSymbol.Outline>
							</esri:SimpleFillSymbol>
						</esri:SimpleRenderer>
					</esri:GraphicsOverlay.Renderer>
				</esri:GraphicsOverlay>
			</esri:MapView.GraphicsOverlays>
        </esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				 HorizontalAlignment="Right" VerticalAlignment="Top"
				 Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <TextBlock Text="Create polygons on the map. The calculated position of a Label Point will be displayed for each polygon."
                           Width="400" TextAlignment="Left" TextWrapping="Wrap" />
                <Button Content="Reset" Margin="12,12,12,0" HorizontalAlignment="Center" Click="ResetButton_Click"/>
            </StackPanel>
        </Border>

        <Border Background="White" BorderBrush="Black" BorderThickness="2" Margin="25"
                HorizontalAlignment="Center" VerticalAlignment="Bottom"
                Visibility="{Binding ElementName=MyMapView, Path=Editor.IsActive, Converter={StaticResource BooleanToVisibilityConverter}}">
            <TextBlock Text="Digitize a polygon on the map." Margin="8" FontSize="14" />
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// This sample demonstrates use of the GeometryEngine.LabelPoint method to calculate the location of label points.
	/// </summary>
	/// <title>Label Point</title>
	/// <category>Geometry</category>
	public partial class LabelPoint : UserControl
	{
		private PictureMarkerSymbol _pictureMarkerSymbol;
		private GraphicsOverlay _labelOverlay;

		/// <summary>Construct Label Point sample control</summary>
		public LabelPoint()
		{
			InitializeComponent();

			_labelOverlay = MyMapView.GraphicsOverlays["labelGraphicOverlay"];

			MyMapView.SpatialReferenceChanged += MyMapView_SpatialReferenceChanged;
			SetupSymbols();
		}

		// Start accepting user polygons and calculating label points
		async void MyMapView_SpatialReferenceChanged(object sender, EventArgs e)
		{
			MyMapView.SpatialReferenceChanged -= MyMapView_SpatialReferenceChanged;
			await CalculateLabelPointsAsync();
		}

		// Load the picture symbol image
		private async void SetupSymbols()
		{
			try
			{
				_pictureMarkerSymbol = layoutGrid.Resources["PictureMarkerSymbol"] as PictureMarkerSymbol;
				await _pictureMarkerSymbol.SetSourceAsync(new Uri("pack://application:,,,/ArcGISRuntimeSamplesDesktop;component/Assets/x-24x24.png"));
			}
			catch (Exception ex)
			{
				MessageBox.Show("Error occurred : " + ex.Message, "Label Point Sample");
			}
		}


		// Continuously accept polygons from the user and calculate label points
		private async Task CalculateLabelPointsAsync()
		{
			try
			{
				await MyMapView.LayersLoadedAsync();

				while (MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry.Extent != null)
				{
					if (MyMapView.Editor.IsActive)
						MyMapView.Editor.Cancel.Execute(null);

					//Get the input polygon geometry from the user
					var poly = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polygon, ((SimpleRenderer)_labelOverlay.Renderer).Symbol);
					if (poly != null)
					{
						//Add the polygon drawn by the user
						_labelOverlay.Graphics.Add(new Graphic(poly));

						//Get the label point for the input geometry
						var labelPoint = GeometryEngine.LabelPoint(poly);
						if (labelPoint != null)
						{
							_labelOverlay.Graphics.Add(new Graphic(labelPoint, _pictureMarkerSymbol));
						}
					}
				}
			}
			catch (TaskCanceledException) { }
			catch (Exception ex)
			{
				MessageBox.Show("Label Point Error: " + ex.Message, "Label Point Sample");
			}
		}

		// Clear label graphics and restart calculating label points
		private async void ResetButton_Click(object sender, RoutedEventArgs e)
		{
			_labelOverlay.Graphics.Clear();
			await CalculateLabelPointsAsync();
		}
	}
}
Feedback on this topic?