This sample demonstrates simple use of the RouteTask. To use the sample, click points on the map. As new points are added, routes between the new point and the last point are calculated and displayed on the map.
In the code-behind, a RouteTask is used to calculate the route between the clicked points. The route geometry contained in the task results is then simply added to a GraphicsLayer in the map.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.Routing "
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 " >
< Grid x:Name = " LayoutRoot " Background = " White " >
< Grid.Resources >
< esri : SimpleMarkerSymbol x:Key = " StopSymbol " Size = " 15 " Style = " Triangle " Color = " Green " />
< esri : SimpleLineSymbol x:Key = " RouteSymbol " Color = " #990000FF " Width = " 5 " />
< esri : RouteTask
x:Key = " MyRouteTask "
Failed = " MyRouteTask_Failed "
SolveCompleted = " MyRouteTask_SolveCompleted "
Url = " http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route " />
</ Grid.Resources >
< esri : Map x:Name = " MyMap " WrapAround = " True " Background = " White " Extent = " -117.22,34.04,-117.17,34.07 "
MouseClick = " MyMap_MouseClick " >
< esri : Map.Layers >
< esri : ArcGISTiledMapServiceLayer
Url = " http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer " />
< esri : GraphicsLayer ID = " MyStopsGraphicsLayer " />
< esri : GraphicsLayer ID = " MyRouteGraphicsLayer " >
< esri : GraphicsLayer.MapTip >
< Grid >
< Rectangle RadiusX = " 10 " RadiusY = " 10 " Fill = " #22000000 " Margin = " 5,5,-5,-5 " />
< Rectangle RadiusX = " 5 " RadiusY = " 5 " Margin = " 3 " >
< Rectangle.Fill >
< LinearGradientBrush EndPoint = " 1.038,1.136 " StartPoint = " 0.015,0.188 " >
< GradientStop Color = " #FFD1DFF2 " />
< GradientStop Color = " #FF092959 " Offset = " 0.946 " />
</ LinearGradientBrush >
</ Rectangle.Fill >
</ Rectangle >
< Border CornerRadius = " 10 " BorderBrush = " #FF222957 " BorderThickness = " 3 " />
< StackPanel Orientation = " Vertical " Margin = " 15,10,15,10 " >
< TextBlock Text = " Total Time: " Foreground = " #FFFFFFFF " FontSize = " 10 " />
< TextBlock Text = " {Binding [TIP]} " Foreground = " #FFFFFFFF " FontSize = " 10 " />
</ StackPanel >
</ Grid >
</ esri : GraphicsLayer.MapTip >
</ esri : GraphicsLayer >
</ esri : Map.Layers >
</ esri : Map >
</ Grid >
</ UserControl >
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
public partial class Routing : UserControl
{
public Routing()
{
InitializeComponent();
}
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
GraphicsLayer stopsGraphicsLayer = MyMap.Layers["MyStopsGraphicsLayer" ] as GraphicsLayer;
Graphic stop = new Graphic() { Geometry = e.MapPoint, Symbol = LayoutRoot.Resources["StopSymbol" ] as ESRI.ArcGIS.Client.Symbols.Symbol };
stopsGraphicsLayer.Graphics.Add(stop);
if (stopsGraphicsLayer.Graphics.Count > 1)
{
RouteTask routeTask = LayoutRoot.Resources["MyRouteTask" ] as RouteTask;
if (routeTask.IsBusy)
{
routeTask.CancelAsync();
stopsGraphicsLayer.Graphics.RemoveAt(stopsGraphicsLayer.Graphics.Count - 1);
}
routeTask.SolveAsync(new RouteParameters() { Stops = stopsGraphicsLayer, UseTimeWindows = false });
}
}
private void MyRouteTask_Failed(object sender, TaskFailedEventArgs e)
{
string errorMessage = "Routing error: " ;
errorMessage += e.Error.Message;
foreach (string detail in (e.Error as ServiceException).Details)
errorMessage += "," + detail;
MessageBox.Show(errorMessage);
GraphicsLayer stopsGraphicsLayer = MyMap.Layers["MyStopsGraphicsLayer" ] as GraphicsLayer;
stopsGraphicsLayer.Graphics.RemoveAt(stopsGraphicsLayer.Graphics.Count - 1);
}
private void MyRouteTask_SolveCompleted(object sender, RouteEventArgs e)
{
GraphicsLayer routeGraphicsLayer = MyMap.Layers["MyRouteGraphicsLayer" ] as GraphicsLayer;
routeGraphicsLayer.Graphics.Clear();
RouteResult routeResult = e.RouteResults[0];
routeResult.Route.Symbol = LayoutRoot.Resources["RouteSymbol" ] as ESRI.ArcGIS.Client.Symbols.Symbol;
Graphic lastRoute = routeResult.Route;
decimal totalTime = (decimal )lastRoute.Attributes["Total_Time" ];
string tip = string .Format("{0} minutes" , totalTime.ToString("#0.000" ));
lastRoute.Attributes.Add("TIP" , tip);
routeGraphicsLayer.Graphics.Add(lastRoute);
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
Partial Public Class Routing
Inherits UserControl
Public Sub New ()
InitializeComponent()
End Sub
Private Sub MyMap_MouseClick(sender As Object , e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
Dim stopsGraphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyStopsGraphicsLayer" ), GraphicsLayer)
Dim [stop ] As New Graphic() With { _
.Geometry = e.MapPoint, _
.Symbol = TryCast(LayoutRoot.Resources("StopSymbol" ), ESRI.ArcGIS.Client.Symbols.Symbol) _
}
stopsGraphicsLayer.Graphics.Add([stop ])
If stopsGraphicsLayer.Graphics.Count > 1 Then
Dim routeTask As RouteTask = TryCast(LayoutRoot.Resources("MyRouteTask" ), RouteTask)
If routeTask.IsBusy Then
routeTask.CancelAsync()
stopsGraphicsLayer.Graphics.RemoveAt(stopsGraphicsLayer.Graphics.Count - 1)
End If
routeTask.SolveAsync(New RouteParameters() With { _
.Stops = stopsGraphicsLayer, _
.UseTimeWindows = False _
})
End If
End Sub
Private Sub MyRouteTask_Failed(sender As Object , e As TaskFailedEventArgs)
Dim errorMessage As String = "Routing error: "
errorMessage += e.[Error ].Message
For Each detail As String In TryCast(e.[Error ], ServiceException).Details
errorMessage += "," & detail
Next
MessageBox.Show(errorMessage)
Dim stopsGraphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyStopsGraphicsLayer" ), GraphicsLayer)
stopsGraphicsLayer.Graphics.RemoveAt(stopsGraphicsLayer.Graphics.Count - 1)
End Sub
Private Sub MyRouteTask_SolveCompleted(sender As Object , e As RouteEventArgs)
Dim routeGraphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyRouteGraphicsLayer" ), GraphicsLayer)
routeGraphicsLayer.Graphics.Clear()
Dim routeResult As RouteResult = e.RouteResults(0)
routeResult.Route.Symbol = TryCast(LayoutRoot.Resources("RouteSymbol" ), ESRI.ArcGIS.Client.Symbols.Symbol)
Dim lastRoute As Graphic = routeResult.Route
Dim totalTime As Decimal = CDec (lastRoute.Attributes("Total_Time" ))
Dim tip As String = String .Format("{0} minutes" , totalTime.ToString("#0.000" ))
lastRoute.Attributes.Add("TIP" , tip)
routeGraphicsLayer.Graphics.Add(lastRoute)
End Sub
End Class
End Namespace
5/16/2014