Route around barriers

View inMAUIUWPWPFWinUIView on GitHub

Find a route that reaches all stops without crossing any barriers.

Image of route around barriers

Use case

You can define barriers to avoid unsafe areas, for example flooded roads, when planning the most efficient route to evacuate a hurricane zone. When solving a route, barriers allow you to define portions of the road network that cannot be traversed. You could also use this functionality to plan routes when you know an area will be inaccessible due to a community activity like an organized race or a market night.

In some situations, it is further beneficial to find the most efficient route that reaches all stops, reordering them to reduce travel time. For example, a delivery service may target a number of drop-off addresses, specifically looking to avoid congested areas or closed roads, arranging the stops in the most time-effective order.

How to use the sample

Click 'Add stop' to add stops to the route. Click 'Add barrier' to add areas that can't be crossed by the route. Click 'Route' to find the route and display it. Select 'Allow stops to be re-ordered' to find the best sequence. Select 'Preserve first stop' if there is a known start point, and 'Preserve last stop' if there is a known final destination.

How it works

  1. Create the route task by calling RouteTask.CreateAsync(_serviceUrl) with the URL to a Network Analysis route service.
  2. Get the default route parameters for the service by calling _routeTask.CreateDefaultParametersAsync.
  3. When the user adds a stop, add it to the route parameters.
    1. Normalize the geometry; otherwise the route job would fail if the user included any stops over the 180th degree meridian.
    2. Get the name of the stop by counting the existing stops - _stepsOverlay.Graphics.Count + 1.
    3. Create a composite symbol for the stop. This sample uses a pushpin marker and a text symbol.
    4. Create the graphic from the geometry and the symbol.
    5. Add the graphic to the stops graphics overlay.
  4. When the user adds a barrier, create a polygon barrier and add it to the route parameters.
    1. Normalize the geometry (see 3i above).
    2. Buffer the geometry to create a larger barrier from the tapped point by calling GeometryEngine.BufferGeodetic(mapLocation, 500, LinearUnits.Meters).
    3. Create the graphic from the geometry and the symbol.
    4. Add the graphic to the barriers overlay.
  5. When ready to find the route, configure the route parameters.
    1. Set the ReturnStops and ReturnDirections to true.
    2. Create a Stop for each graphic in the stops graphics overlay. Add that stop to a list, then call _routeParameters.SetStops(routeStops).
    3. Create a PolygonBarrier for each graphic in the barriers graphics overlay. Add that barrier to a list, then call _routeParameters.SetPolygonBarriers(routeBarriers).
    4. If the user will accept routes with the stops in any order, set FindBestSequence to true to find the most optimal route.
    5. If the user has a definite start point, set PreserveFirstStop to true.
    6. If the user has a definite final destination, set PreserveLastStop to true.
  6. Calculate and display the route.
    1. Call _routeTask.SolveRouteAsync(_routeParameters) to get a RouteResult.
    2. Get the first returned route by calling calculatedRoute.Routes.First().
    3. Get the geometry from the route as a polyline by accessing the firstResult.RouteGeometry property.
    4. Create a graphic from the polyline and a simple line symbol.
    5. Display the steps on the route, available from firstResult.DirectionManeuvers.

Relevant API

  • DirectionManeuver
  • PolygonBarrier
  • Route
  • Route.DirectionManeuver
  • Route.RouteGeometry
  • RouteParameters.ClearPolygonBarriers
  • RouteParameters.FindBestSequence
  • RouteParameters.PreserveFirstStop
  • RouteParameters.PreserveLastStop
  • RouteParameters.ReturnDirections
  • RouteParameters.ReturnStops
  • RouteParameters.SetPolygonBarriers
  • RouteResult
  • RouteResult.Routes
  • RouteTask
  • Stop
  • Stop.Name

About the data

This sample uses an Esri-hosted sample street network for San Diego.

Tags

barriers, best sequence, directions, maneuver, network analysis, routing, sequence, stop order, stops

Sample Code

RouteAroundBarriers.xamlRouteAroundBarriers.xamlRouteAroundBarriers.xaml.cs
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<UserControl
    x:Class="ArcGIS.UWP.Samples.RouteAroundBarriers.RouteAroundBarriers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:networkAnalysis="using:Esri.ArcGISRuntime.Tasks.NetworkAnalysis"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <Grid>
        <esriUI:MapView x:Name="MyMapView"
                        GeoViewTapped="MyMapView_OnGeoViewTapped" />
        <Border Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="150" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="StatusLabel"
                           Margin="0,0,0,5"
                           Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
                           FontWeight="SemiBold"
                           TextWrapping="Wrap" />
                <Button x:Name="AddStopButton"
                        Content="Add stop"
                        HorizontalAlignment="Stretch"
                        Grid.Row="1" Grid.Column="0"
                        Click="AddStop_Clicked" />
                <Button x:Name="AddBarrierButton"
                        Content="Add barrier"
                        Margin="5,0,5,0"
                        HorizontalAlignment="Stretch"
                        Grid.Row="1" Grid.Column="1"
                        Click="AddBarrier_Clicked" />
                <Button x:Name="ResetRoutingButton"
                        Content="Reset"
                        HorizontalAlignment="Stretch"
                        Grid.Row="1" Grid.Column="2"
                        Click="ResetRoute_Clicked" />
                <CheckBox x:Name="AllowReorderStopsCheckbox"
                          Content="Allow stops to be re-ordered"
                          Margin="0,5,0,0"
                          Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" />
                <CheckBox x:Name="PreserveFirstStopCheckbox"
                          Content="Preserve first stop"
                          Margin="20,5,5,0"
                          Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" />
                <CheckBox x:Name="PreserveLastStopCheckbox"
                          Content="Preserve last stop"
                          Margin="20,5,5,0"
                          Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3" />
                <Button x:Name="CalculateRouteButton"
                        Content="Route"
                        Margin="0,5,0,5"
                        HorizontalAlignment="Stretch"
                        Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3"
                        Click="RouteButton_Clicked" />
                <ListBox x:Name="DirectionsListBox"
                         Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="3">
                    <ListBox.ItemTemplate>
                        <DataTemplate x:DataType="networkAnalysis:DirectionManeuver">
                            <TextBlock Text="{Binding DirectionText}" TextWrapping="Wrap" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <Grid x:Name="BusyOverlay"
                      Grid.Row="0" Grid.RowSpan="7" Grid.Column="0" Grid.ColumnSpan="3"
                      Margin="-20"
                      Visibility="Collapsed"
                      Background="#aa000000">
                    <TextBlock Text="Calculating route"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Foreground="White" />
                </Grid>
            </Grid>
        </Border>
    </Grid>
</UserControl>

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.