This sample demonstrates use of a GeometryService to measure distances. To use the sample, click subsequent points on the map to draw a line. Double-click to complete the line. When the line is completed, it's distance will be calculated and displayed in the upper right corner of the application.
In the code-behind, a GeometryService is used to first project the line into a spatial reference that uses measured units, and then to calculate the length of the line.
<UserControlx:Class="ArcGISWPFSDK.Lengths"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"><Gridx:Name="LayoutRoot"><Grid.Resources><esri:SimpleLineSymbolx:Key="DefaultLineSymbol"Color="Red"Width="4"/><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"Offset="0.16"/><GradientStopColor="#FF3D7FAC"Offset="0.502"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><esri:Mapx:Name="MyMap"Extent="-118.331,33.7,-116.75,34"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/><esri:GraphicsLayerID="MyGraphicsLayer"/></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,10,10,0"><RectangleStroke="Gray"RadiusX="10"RadiusY="10"Fill="{StaticResource PanelGradient}"Margin="0,0,0,5"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><RectangleFill="#FFFFFFFF"Stroke="DarkGray"RadiusX="5"RadiusY="5"Margin="10,10,10,15"/><TextBlockx:Name="ResponseTextBlock"Text="Create a polyline by clicking on the map. The distance is displayed here."Width="200"TextAlignment="Left"Margin="30,20,30,30"TextWrapping="Wrap"Foreground="Black"/></Grid></Grid></UserControl>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass Lengths : UserControl
{
private Draw MyDrawObject;
public Lengths()
{
InitializeComponent();
MyDrawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true,
LineSymbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol
};
MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
MyDrawObject.DrawBegin += MyDrawObject_DrawBegin;
}
privatevoid MyDrawObject_DrawBegin(object sender, EventArgs args)
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
}
privatevoid MyDrawObject_DrawComplete(object sender, DrawEventArgs args)
{
ESRI.ArcGIS.Client.Geometry.Polyline polyline = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polyline;
polyline.SpatialReference = MyMap.SpatialReference;
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = polyline
};
GeometryService geometryService =
new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
geometryService.LengthsCompleted += GeometryService_LengthsCompleted;
geometryService.Failed += GeometryService_Failed;
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.Graphics.Add(graphic);
geometryService.LengthsAsync(graphicsLayer.Graphics.ToList(), LinearUnit.SurveyMile, CalculationType.Geodesic, null);
}
privatevoid GeometryService_LengthsCompleted(object sender, ESRI.ArcGIS.Client.Tasks.LengthsEventArgs args)
{
ResponseTextBlock.Text =
String.Format("The distance of the polyline is {0} miles", Math.Round(args.Results[0], 3));
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
}
}
}
Imports System.Linq
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass Lengths
Inherits UserControl
Private MyDrawObject As Draw
PublicSubNew()
InitializeComponent()
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Polyline, _
.IsEnabled = True, _
.LineSymbol = TryCast(LayoutRoot.Resources("DefaultLineSymbol"), ESRI.ArcGIS.Client.Symbols.LineSymbol) _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
AddHandler MyDrawObject.DrawBegin, AddressOf MyDrawObject_DrawBegin
EndSubPrivateSub MyDrawObject_DrawBegin(sender AsObject, args As EventArgs)
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.ClearGraphics()
EndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
Dim polyline As ESRI.ArcGIS.Client.Geometry.Polyline = TryCast(args.Geometry, ESRI.ArcGIS.Client.Geometry.Polyline)
polyline.SpatialReference = MyMap.SpatialReference
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultLineSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = polyline _
}
Dim geometryService AsNew GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.LengthsCompleted, AddressOf GeometryService_LengthsCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.Graphics.Add(graphic)
geometryService.LengthsAsync(graphicsLayer.Graphics.ToList(), LinearUnit.SurveyMile, CalculationType.Geodesic, Nothing)
EndSubPrivateSub GeometryService_LengthsCompleted(sender AsObject, args As ESRI.ArcGIS.Client.Tasks.LengthsEventArgs)
ResponseTextBlock.Text = [String].Format("The distance of the polyline is {0} miles", Math.Round(args.Results(0), 3))
EndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace