This sample demonstrates use of the RouteTask to calculate routes when barriers exist. To use the sample, select whether to add stops or add barriers from the dialog in the upper right of the application, then click points on the map. Each time you click, a barrier or stop will be added. When a new stop is added, the route between the new stop and the last stop will be calculated and shown on the map.
In the code-behind, a RouteTask is used to calculate the route between the stops. The barriers are maintained in a list of graphics and specified in the RouteTask's parameters when a route operation is executed. When route results are returned, the route geometry is added to the map.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.RoutingBarriers "
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 >
< ControlTemplate x:Key = " CompositeSymbol " >
< Grid >
< Ellipse Fill = " {Binding Symbol.Color} " Width = " {Binding Symbol.Size} " Height = " {Binding Symbol.Size} " Stroke = " Black " StrokeThickness = " 1 " />
< TextBlock HorizontalAlignment = " Center " VerticalAlignment = " Center "
Text = " {Binding Path=Attributes[StopNumber]} "
FontSize = " 9 " Margin = " 0 " FontWeight = " Bold " Foreground = " Black " />
</ Grid >
</ ControlTemplate >
< esri : SimpleMarkerSymbol x:Key = " StopSymbol " Size = " 20 " Style = " Circle " Color = " Salmon " ControlTemplate = " {StaticResource CompositeSymbol} " />
< esri : SimpleMarkerSymbol x:Key = " BarrierSymbol " Size = " 15 " Style = " Square " Color = " Red " />
< esri : SimpleLineSymbol x:Key = " RouteSymbol " Color = " #990000FF " Width = " 5 " />
< LinearGradientBrush x:Key = " PanelGradient " EndPoint = " 0.5,1 " StartPoint = " 0.5,0 " >
< LinearGradientBrush.RelativeTransform >
< TransformGroup >
< ScaleTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< SkewTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< RotateTransform Angle = " 176 " CenterY = " 0.5 " CenterX = " 0.5 " />
< TranslateTransform />
</ TransformGroup >
</ LinearGradientBrush.RelativeTransform >
< GradientStop Color = " #FF145787 " Offset = " 0.16 " />
< GradientStop Color = " #FF3D7FAC " Offset = " 0.502 " />
< GradientStop Color = " #FF88C5EF " Offset = " 0.984 " />
</ LinearGradientBrush >
</ Grid.Resources >
< esri : Map x:Name = " MyMap " 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 = " MyRouteGraphicsLayer " />
< esri : GraphicsLayer ID = " MyStopsGraphicsLayer " />
< esri : GraphicsLayer ID = " MyBarriersGraphicsLayer " />
</ esri : Map.Layers >
</ esri : Map >
< Grid HorizontalAlignment = " Right " VerticalAlignment = " Top " Margin = " 10 " >
< Rectangle Fill = " {StaticResource PanelGradient} " Stroke = " Gray " RadiusX = " 10 " RadiusY = " 10 " Margin = " 0 " >
< Rectangle.Effect >
< DropShadowEffect />
</ Rectangle.Effect >
</ Rectangle >
< StackPanel Orientation = " Horizontal " Margin = " 5 " >
< RadioButton Content = " Add Stops " x:Name = " StopsRadioButton " IsChecked = " true "
Foreground = " White " GroupName = " add " VerticalAlignment = " Center " />
< RadioButton Content = " Add Barriers " x:Name = " BarriersRadioButton "
Foreground = " White " GroupName = " add " VerticalAlignment = " Center " />
< Button Content = " Clear " Click = " Button_Click " Margin = " 5,0,0,0 " />
</ StackPanel >
</ Grid >
</ Grid >
</ UserControl >
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
public partial class RoutingBarriers : UserControl
{
RouteTask _routeTask;
List<Graphic> _stops = new List<Graphic>();
List<Graphic> _barriers = new List<Graphic>();
RouteParameters _routeParams = new RouteParameters();
GraphicsLayer stopsLayer = null ;
GraphicsLayer barriersLayer = null ;
public RoutingBarriers()
{
InitializeComponent();
_routeTask =
new RouteTask("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route" );
_routeTask.SolveCompleted += routeTask_SolveCompleted;
_routeTask.Failed += routeTask_Failed;
_routeParams.Stops = _stops;
_routeParams.Barriers = _barriers;
_routeParams.UseTimeWindows = false ;
barriersLayer = MyMap.Layers["MyBarriersGraphicsLayer" ] as GraphicsLayer;
stopsLayer = MyMap.Layers["MyStopsGraphicsLayer" ] as GraphicsLayer;
}
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
if (StopsRadioButton.IsChecked.Value)
{
Graphic stop = new Graphic() { Geometry = e.MapPoint, Symbol = LayoutRoot.Resources["StopSymbol" ] as ESRI.ArcGIS.Client.Symbols.Symbol };
stop.Attributes.Add("StopNumber" , stopsLayer.Graphics.Count + 1);
stopsLayer.Graphics.Add(stop);
_stops.Add(stop);
}
else if (BarriersRadioButton.IsChecked.Value)
{
Graphic barrier = new Graphic() { Geometry = e.MapPoint, Symbol = LayoutRoot.Resources["BarrierSymbol" ] as ESRI.ArcGIS.Client.Symbols.Symbol };
barriersLayer.Graphics.Add(barrier);
_barriers.Add(barrier);
}
if (_stops.Count > 1)
{
if (_routeTask.IsBusy)
_routeTask.CancelAsync();
_routeTask.SolveAsync(_routeParams);
}
}
private void routeTask_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);
if ((_stops.Count) > 10)
{
stopsLayer.Graphics.RemoveAt(stopsLayer.Graphics.Count - 1);
_stops.RemoveAt(_stops.Count - 1);
}
}
private void routeTask_SolveCompleted(object sender, RouteEventArgs e)
{
GraphicsLayer routeLayer = MyMap.Layers["MyRouteGraphicsLayer" ] as GraphicsLayer;
RouteResult routeResult = e.RouteResults[0];
routeResult.Route.Symbol = LayoutRoot.Resources["RouteSymbol" ] as ESRI.ArcGIS.Client.Symbols.Symbol;
routeLayer.Graphics.Clear();
Graphic lastRoute = routeResult.Route;
routeLayer.Graphics.Add(lastRoute);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_stops.Clear();
_barriers.Clear();
foreach (Layer layer in MyMap.Layers)
if (layer is GraphicsLayer)
(layer as GraphicsLayer).ClearGraphics();
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
Partial Public Class RoutingBarriers
Inherits UserControl
Private _routeTask As RouteTask
Private _stops As New List(Of Graphic)()
Private _barriers As New List(Of Graphic)()
Private _routeParams As New RouteParameters()
Private stopsLayer As GraphicsLayer = Nothing
Private barriersLayer As GraphicsLayer = Nothing
Public Sub New ()
InitializeComponent()
_routeTask = New RouteTask("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route" )
AddHandler _routeTask.SolveCompleted, AddressOf routeTask_SolveCompleted
AddHandler _routeTask.Failed, AddressOf routeTask_Failed
_routeParams.Stops = _stops
_routeParams.Barriers = _barriers
_routeParams.UseTimeWindows = False
barriersLayer = TryCast(MyMap.Layers("MyBarriersGraphicsLayer" ), GraphicsLayer)
stopsLayer = TryCast(MyMap.Layers("MyStopsGraphicsLayer" ), GraphicsLayer)
End Sub
Private Sub MyMap_MouseClick(sender As Object , e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
If StopsRadioButton.IsChecked.Value Then
Dim [stop ] As New Graphic() With { _
.Geometry = e.MapPoint, _
.Symbol = TryCast(LayoutRoot.Resources("StopSymbol" ), ESRI.ArcGIS.Client.Symbols.Symbol) _
}
[stop ].Attributes.Add("StopNumber" , stopsLayer.Graphics.Count + 1)
stopsLayer.Graphics.Add([stop ])
_stops.Add([stop ])
ElseIf BarriersRadioButton.IsChecked.Value Then
Dim barrier As New Graphic() With { _
.Geometry = e.MapPoint, _
.Symbol = TryCast(LayoutRoot.Resources("BarrierSymbol" ), ESRI.ArcGIS.Client.Symbols.Symbol) _
}
barriersLayer.Graphics.Add(barrier)
_barriers.Add(barrier)
End If
If _stops.Count > 1 Then
If _routeTask.IsBusy Then
_routeTask.CancelAsync()
End If
_routeTask.SolveAsync(_routeParams)
End If
End Sub
Private Sub routeTask_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)
If (_stops.Count ) > 10 Then
stopsLayer.Graphics.RemoveAt(stopsLayer.Graphics.Count - 1)
_stops.RemoveAt(_stops.Count - 1)
End If
End Sub
Private Sub routeTask_SolveCompleted(sender As Object , e As RouteEventArgs)
Dim routeLayer As GraphicsLayer = TryCast(MyMap.Layers("MyRouteGraphicsLayer" ), GraphicsLayer)
Dim routeResult As RouteResult = e.RouteResults(0)
routeResult.Route.Symbol = TryCast(LayoutRoot.Resources("RouteSymbol" ), ESRI.ArcGIS.Client.Symbols.Symbol)
routeLayer.Graphics.Clear()
Dim lastRoute As Graphic = routeResult.Route
routeLayer.Graphics.Add(lastRoute)
End Sub
Private Sub Button_Click(sender As Object , e As RoutedEventArgs)
_stops.Clear()
_barriers.Clear()
For Each layer As Layer In MyMap.Layers
If TypeOf layer Is GraphicsLayer Then
TryCast(layer, GraphicsLayer).ClearGraphics()
End If
Next
End Sub
End Class
End Namespace
5/16/2014