This sample demonstrates using an ESRI.ArcGIS.Client.Tasks GeometryService 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.Distance"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="-112.461447, 33.393199, -111.564157, 33.66974"><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"><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.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Geometry;
namespace ArcGISWPFSDK
{
publicpartialclass Distance : UserControl
{
private Draw MyDrawObject;
GraphicsLayer graphicsLayer;
public Distance()
{
InitializeComponent();
graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
MyDrawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true
};
MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
MyDrawObject.DrawBegin += MyDrawObject_DrawBegin;
}
privatevoid MyDrawObject_DrawBegin(object sender, EventArgs args)
{
if (graphicsLayer.Graphics.Count >= 2)
graphicsLayer.ClearGraphics();
}
privatevoid MyDrawObject_DrawComplete(object sender, DrawEventArgs args)
{
//ESRI.ArcGIS.Client.Geometry.Polyline polyline = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polyline;
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;
graphicsLayer.Graphics.Add(graphic);
if (graphicsLayer.Graphics.Count == 1)
MyDrawObject.DrawMode = DrawMode.Point;
elseif (graphicsLayer.Graphics.Count == 2)
{
MyDrawObject.IsEnabled = false;
GeometryService geometryService =
new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
geometryService.DistanceCompleted += GeometryService_DistanceCompleted;
geometryService.Failed += GeometryService_Failed;
MyDrawObject.DrawMode = DrawMode.Polyline;
DistanceParameters distanceParameters = new DistanceParameters()
{
DistanceUnit = LinearUnit.SurveyMile,
Geodesic = true
};
geometryService.DistanceAsync(graphicsLayer.Graphics[0].Geometry, graphicsLayer.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} miles", Math.Round(e.Distance, 3));
MyDrawObject.IsEnabled = true;
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
MyDrawObject.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
Namespace ArcGISWPFSDK
PartialPublicClass Distance
Inherits UserControl
Private MyDrawObject As Draw
Private graphicsLayer As GraphicsLayer
PublicSubNew()
InitializeComponent()
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
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 = FalseDim geometryService AsNew GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.DistanceCompleted, AddressOf GeometryService_DistanceCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
MyDrawObject.DrawMode = DrawMode.Polyline
Dim distanceParameters AsNew DistanceParameters() With { _
.DistanceUnit = LinearUnit.SurveyMile, _
.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} miles", Math.Round(e.Distance, 3))
MyDrawObject.IsEnabled = TrueEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
MyDrawObject.IsEnabled = TrueEndSubEndClassEndNamespace