This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation 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 LocalGeometryService
created and the url is passed into a GeometryService which 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.LocalLengths"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="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><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;
using ESRI.ArcGIS.Client.Local;
namespace ArcGISWPFSDK
{
publicpartialclass LocalLengths : UserControl
{
private Draw _drawObject;
GeometryService _geometryTask;
public LocalLengths()
{
InitializeComponent();
LocalGeometryService.GetServiceAsync(lgs =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = lgs.UrlGeometryService;
_geometryTask.LengthsCompleted += GeometryService_LengthsCompleted;
_geometryTask.Failed += GeometryService_Failed;
_geometryTask.Failed += GeometryService_Failed;
});
MyMap.Layers.LayersInitialized += (s, e) =>
{
_drawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true,
LineSymbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol
};
_drawObject.DrawComplete += DrawObject_DrawComplete;
_drawObject.DrawBegin += DrawObject_DrawBegin;
};
}
privatevoid DrawObject_DrawBegin(object sender, EventArgs args)
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
}
privatevoid DrawObject_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
};
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.Graphics.Add(graphic);
_geometryTask.LengthsAsync(graphicsLayer.Graphics.ToList(), LinearUnit.Kilometer, 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} km", 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
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalLengths
Inherits UserControl
Private MyDrawObject As Draw
Private _geometryService As GeometryService
PublicSubNew()
InitializeComponent()
Dim localGeometryService AsNew LocalGeometryService()
AddHandler localGeometryService.StartCompleted, Function(sender, eventargs)
Dim lgs As LocalGeometryService = TryCast(sender, LocalGeometryService)
_geometryService = New GeometryService()
_geometryService.Url = lgs.UrlGeometryService
MyMap.IsEnabled = TrueAddHandler _geometryService.LengthsCompleted, AddressOf GeometryService_LengthsCompleted
AddHandler _geometryService.Failed, AddressOf GeometryService_Failed
AddHandler _geometryService.Failed, AddressOf GeometryService_Failed
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
EndFunction
localGeometryService.StartAsync()
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 graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.Graphics.Add(graphic)
_geometryService.LengthsAsync(graphicsLayer.Graphics.ToList(), LinearUnit.Kilometer, 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} km", Math.Round(args.Results(0), 3))
EndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace