This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation that takes an input graphic polyline and point and returns the linear difference of the shortest length between the two graphics.
To use the sample, click on the map to add a polyline then add a point. After the point is entered the shortest distance between them is displayed in the textblock on the map.
<UserControlx:Class="ArcGISWPFSDK.LocalDistance"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:SimpleMarkerSymbolx:Key="DefaultMarkerSymbol"Size="6"><esri:SimpleMarkerSymbol.Color><RadialGradientBrush><GradientStopColor="#FFFF0000"Offset="0.578"/><GradientStopColor="#FF0E0D07"Offset="1"/></RadialGradientBrush></esri:SimpleMarkerSymbol.Color></esri:SimpleMarkerSymbol><esri:SimpleLineSymbolx:Key="DefaultLineSymbol"Width="2"Color="Red"/><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="DistanceGraphicsLayer"x:Name="_distanceGraphicsLayer"/></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,10,10,0"><RectangleFill="{StaticResource PanelGradient}"Stroke="Gray"RadiusX="10"RadiusY="10"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="Click on the map to add a polyline, then add a point. After the point is entered the shortest distance between them is displayed here."Width="200"TextAlignment="Left"Margin="30,20,30,30"TextWrapping="Wrap"Foreground="Black"/></Grid></Grid></UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Local;
namespace ArcGISWPFSDK
{
publicpartialclass LocalDistance : UserControl
{
private Draw _drawObject;
GeometryService _geometryTask;
public LocalDistance()
{
InitializeComponent();
LocalGeometryService.GetServiceAsync(lgs =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = lgs.UrlGeometryService;
MyMap.IsEnabled = true;
_geometryTask.DistanceCompleted += GeometryService_DistanceCompleted;
_geometryTask.Failed += GeometryService_Failed;
});
MyMap.Layers.LayersInitialized += (s, e) =>
{
_drawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true
};
_drawObject.DrawComplete += DrawObject_DrawComplete;
_drawObject.DrawBegin += DrawObject_DrawBegin;
};
}
privatevoid DrawObject_DrawBegin(object sender, EventArgs args)
{
if (_distanceGraphicsLayer.Graphics.Count >= 2)
_distanceGraphicsLayer.ClearGraphics();
}
privatevoid DrawObject_DrawComplete(object sender, DrawEventArgs args)
{
args.Geometry.SpatialReference = MyMap.SpatialReference;
Graphic graphic = new Graphic() { Geometry = args.Geometry };
if (args.Geometry is Polyline)
graphic.Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
else
graphic.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
_distanceGraphicsLayer.Graphics.Add(graphic);
if (_distanceGraphicsLayer.Graphics.Count == 1)
_drawObject.DrawMode = DrawMode.Point;
elseif (_distanceGraphicsLayer.Graphics.Count == 2)
{
_drawObject.IsEnabled = false;
_drawObject.DrawMode = DrawMode.Polyline;
DistanceParameters distanceParameters = new DistanceParameters()
{
DistanceUnit = LinearUnit.Kilometer,
Geodesic = true
};
_geometryTask.DistanceAsync(_distanceGraphicsLayer.Graphics[0].Geometry, _distanceGraphicsLayer.Graphics[1].Geometry, distanceParameters);
ResponseTextBlock.Text = "The distance between geometries is... ";
}
}
void GeometryService_DistanceCompleted(object sender, DistanceEventArgs e)
{
ResponseTextBlock.Text =
String.Format("The distance between geometries is {0} km", (e.Distance));
_drawObject.IsEnabled = true;
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
_drawObject.IsEnabled = true;
}
}
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalDistance
Inherits UserControl
Private MyDrawObject As Draw
Private graphicsLayer As GraphicsLayer
Private _geometryService As GeometryService
PublicSubNew()
InitializeComponent()
Dim localGeometryService AsNew LocalGeometryService()
AddHandler localGeometryService.StartCompleted, AddressOf localGeometryService_StartCompleted
localGeometryService.StartAsync()
EndSubPrivateSub MyDrawObject_DrawBegin(sender AsObject, args As EventArgs)
If graphicsLayer.Graphics.Count >= 2 Then
graphicsLayer.ClearGraphics()
EndIfEndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
'ESRI.ArcGIS.Client.Geometry.Polyline polyline = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polyline;
args.Geometry.SpatialReference = MyMap.SpatialReference
Dim graphic AsNew Graphic() With { _
.Geometry = args.Geometry _
}
IfTypeOf args.Geometry Is Polyline Then
graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultLineSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
Else
graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
EndIf
graphicsLayer.Graphics.Add(graphic)
If graphicsLayer.Graphics.Count = 1 Then
MyDrawObject.DrawMode = DrawMode.Point
ElseIf graphicsLayer.Graphics.Count = 2 Then
MyDrawObject.IsEnabled = False
MyDrawObject.DrawMode = DrawMode.Polyline
Dim distanceParameters AsNew DistanceParameters() With { _
.DistanceUnit = LinearUnit.Kilometer, _
.Geodesic = True _
}
_geometryService.DistanceAsync(graphicsLayer.Graphics(0).Geometry, graphicsLayer.Graphics(1).Geometry, distanceParameters)
ResponseTextBlock.Text = "The distance between geometries is... "EndIfEndSubPrivateSub GeometryService_DistanceCompleted(sender AsObject, e As DistanceEventArgs)
ResponseTextBlock.Text = [String].Format("The distance between geometries is {0} km", (e.Distance))
MyDrawObject.IsEnabled = TrueEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
MyDrawObject.IsEnabled = TrueEndSubPrivateSub localGeometryService_StartCompleted(sender AsObject, e As ComponentModel.AsyncCompletedEventArgs)
Dim lgs As LocalGeometryService = TryCast(sender, LocalGeometryService)
_geometryService = New GeometryService()
_geometryService.Url = lgs.UrlGeometryService
MyMap.IsEnabled = TrueAddHandler _geometryService.DistanceCompleted, AddressOf GeometryService_DistanceCompleted
AddHandler _geometryService.Failed, AddressOf GeometryService_Failed
graphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Polyline, _
.IsEnabled = True _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
AddHandler MyDrawObject.DrawBegin, AddressOf MyDrawObject_DrawBegin
EndSubEndClassEndNamespace