Find a route that reaches all stops without crossing any barriers.
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.
Tap 'Add stop' to add stops to the route. Tap 'Add barrier' to add areas that can't be crossed by the route. Tap '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.
- Create the route task by calling
RouteTask.CreateAsync(_serviceUrl)
with the URL to a Network Analysis route service. - Get the default route parameters for the service by calling
_routeTask.CreateDefaultParametersAsync
. - When the user adds a stop, add it to the route parameters.
- Normalize the geometry; otherwise the route job would fail if the user included any stops over the 180th degree meridian.
- Get the name of the stop by counting the existing stops -
_stepsOverlay.Graphics.Count + 1
. - Create a composite symbol for the stop. This sample uses a pushpin marker and a text symbol.
- Create the graphic from the geometry and the symbol.
- Add the graphic to the stops graphics overlay.
- When the user adds a barrier, create a polygon barrier and add it to the route parameters.
- Normalize the geometry (see 3i above).
- Buffer the geometry to create a larger barrier from the tapped point by calling
mapLocation.BufferGeodetic(500, LinearUnits.Meters)
. - Create the graphic from the geometry and the symbol.
- Add the graphic to the barriers overlay.
- When ready to find the route, configure the route parameters.
- Set the
ReturnStops
andReturnDirections
totrue
. - Create a
Stop
for each graphic in the stops graphics overlay. Add that stop to a list, then call_routeParameters.SetStops(routeStops)
. - Create a
PolygonBarrier
for each graphic in the barriers graphics overlay. Add that barrier to a list, then call_routeParameters.SetPolygonBarriers(routeBarriers)
. - If the user will accept routes with the stops in any order, set
FindBestSequence
totrue
to find the most optimal route. - If the user has a definite start point, set
PreserveFirstStop
totrue
. - If the user has a definite final destination, set
PreserveLastStop
totrue
.
- Set the
- Calculate and display the route.
- Call
_routeTask.SolveRouteAsync(_routeParameters)
to get aRouteResult
. - Get the first returned route by calling
calculatedRoute.Routes.First()
. - Get the geometry from the route as a polyline by accessing the
firstResult.RouteGeometry
property. - Create a graphic from the polyline and a simple line symbol.
- Display the steps on the route, available from
firstResult.DirectionManeuvers
.
- Call
- 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
This sample uses an Esri-hosted sample street network for San Diego.
barriers, best sequence, directions, maneuver, network analysis, routing, sequence, stop order, stops