Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Line Length

Download Samples Repository

Description

This sample demonstrates use of the GeometryEngine.GeodesicLength method to calculate the length of a line.

"Phone" Available for Phone

Sample Code

<Page
	x:Class="ArcGISRuntime.Samples.Phone.Samples.LineLength"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	xmlns:esri="using:Esri.ArcGISRuntime.Controls"  
	xmlns:layers="using:Esri.ArcGISRuntime.Layers"
	xmlns:symb="using:Esri.ArcGISRuntime.Symbology"
	xmlns:geom="using:Esri.ArcGISRuntime.Geometry"
	mc:Ignorable="d">

	<Grid x:Name="LayoutRoot" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
		<Grid.Resources>
			<symb:PictureMarkerSymbol x:Key="MyPictureMarkerSymbol" XOffset="12" YOffset="12" />
		</Grid.Resources>
		<esri:MapView x:Name="MyMapView" LayerLoaded="MyMapView_LayerLoaded">
			<esri:Map>
				<layers:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
												   ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
				<layers:GraphicsLayer ID="MyGraphicsLayer" >
					<layers:GraphicsLayer.Renderer>
						<symb:SimpleRenderer>
							<symb:SimpleFillSymbol  Color="#7F0000FF"    >
								<symb:SimpleFillSymbol.Outline>
									<symb:SimpleLineSymbol Color="Blue" Width="2"/>
								</symb:SimpleFillSymbol.Outline>
							</symb:SimpleFillSymbol>
						</symb:SimpleRenderer>
					</layers:GraphicsLayer.Renderer>
				</layers:GraphicsLayer>
			</esri:Map>
		</esri:MapView>

		<Grid HorizontalAlignment="Right" 
			  VerticalAlignment="Top" 
			  Margin="0,15,15,0" >
			<Border Background="Black" BorderThickness="1" 
					BorderBrush="White" Padding="30,20,30,30">

				<StackPanel Orientation="Vertical" >
					<TextBlock x:Name="InstructionsTextBlock" FontSize="14" 
							   Text="Create a polyline by clicking on map. The calculated geodesic length will be displayed." 
							   Visibility="Visible" Width="250" 
							   TextAlignment="Left" TextWrapping="Wrap" 
							   HorizontalAlignment="Left" VerticalAlignment="Center"/>

					<TextBlock x:Name="LineLengthTextBlock" FontSize="30" 
							   Foreground="Bisque" Visibility="Collapsed"
							   TextAlignment="Center" TextWrapping="Wrap" 
							   VerticalAlignment="Center" Margin="0,10,0,0"/>

					<Button x:Name="ResetButton" Content="Reset" 
							Click="ResetButton_Click"  Width="250" 
							Margin="0,15,0,0" IsEnabled="False"/>

				</StackPanel>
			</Border>
		</Grid>
	</Grid>
</Page>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace ArcGISRuntime.Samples.Phone.Samples
{
	/// <summary>
	/// This sample demonstrates use of the GeometryEngine.GeodesicLength method to calculate the length of a line.
	/// </summary>
	/// <title>Line Length</title>
	/// <category>Geometry</category>
	public sealed partial class LineLength : Page
	{
		GraphicsLayer myGraphicsLayer;
		Geometry inputGeom;
		public LineLength()
		{
			InitializeComponent();

			MyMapView.Map.InitialViewpoint = new Viewpoint(new Envelope(-13149423, 3997267, -12992880, 4062214, SpatialReferences.WebMercator));
			myGraphicsLayer = MyMapView.Map.Layers["MyGraphicsLayer"] as GraphicsLayer;
		}

		private async Task DoGeodesicLength()
		{
			ResetButton.IsEnabled = false;

			try
			{
				if (MyMapView.Editor.IsActive)
					MyMapView.Editor.Cancel.Execute(null);

				//Get the input polygon geometry from the user
				inputGeom = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polyline);

				if (inputGeom != null)
				{
					//Add the polygon drawn by the user
					var g = new Graphic
					{
						Geometry = inputGeom,
					};
					myGraphicsLayer.Graphics.Add(g);


					//Get the label point for the input geometry
					var length = GeometryEngine.GeodesicLength(inputGeom);
					LineLengthTextBlock.Text = length.ToString("N2") + " m";
					LineLengthTextBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
				}
			}
			catch (Exception)
			{

			}
			ResetButton.IsEnabled = true;

		}

		private async void ResetButton_Click(object sender, RoutedEventArgs e)
		{
			LineLengthTextBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

			myGraphicsLayer.Graphics.Clear();
			await DoGeodesicLength();

		}


		private async void MyMapView_LayerLoaded(object sender, LayerLoadedEventArgs e)
		{
			if (e.Layer.ID == "MyGraphicsLayer")
			{
				await DoGeodesicLength();
			}
		}
	}
}
Feedback on this topic?