View on GitHub Sample viewer app

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

Image of routing 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

Use the “Edit Mode” toggle buttons to select whether to add Stops or Barriers to the route. The route will be solved automatically as you add stops and barriers, and information about the length of the route and directions will be shown in the controls area. Select “Find best sequence” to allow stops to be re-ordered in order to find an optimum route. Select “Preserve first stop” to preserve the first stop. Select “Preserve last stop” to preserve the last stop. You can use the “Reset” button to reset the sample.

How it works

  1. Create a RouteTask with the URL to a Network Analysis route service.
  2. Get the default RouteParameters for the service, and create the desired Stops and PolygonBarriers.
  3. Add the stops and barriers to the route’s parameters, routeParameters.setStops(routeStops) and routeParameters.setPolygonBarriers(routeBarriers).
  4. Set the ReturnStops and ReturnDirections to true.
  5. If the user will accept routes with the stops in any order, set FindBestSequence to true to find the most optimal route.
  6. If the user has a definite start point, set PreserveFirstStop to true.
  7. If the user has a definite final destination, set PreserveLastStop to true.
  8. Call routeTask.solveRouteAsync(routeParameters) to get a RouteResult.
  9. Get the first returned route by calling routeResult.getRoutes().get(0).
  10. Get the geometry from the route to display the route to the map.

Relevant API

  • DirectionManeuver
  • PolygonBarrier
  • Route
  • RouteParameters
  • RouteResult
  • RouteTask
  • Stop

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

module-info.java module-info.java RoutingAroundBarriersController.java RoutingAroundBarriersSample.java
/*
* Copyright 2022 Esri.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module com.esri.samples.routing_around_barriers {
// require ArcGIS Maps SDK for Java module
requires com.esri.arcgisruntime;
// handle SLF4J http://www.slf4j.org/codes.html#StaticLoggerBinder
requires org.slf4j.nop;
// require JavaFX modules that the application uses
requires javafx.graphics;
requires javafx.controls;
requires javafx.fxml;
// make all @FXML annotated objects reflectively accessible to the javafx.fxml module
opens com.esri.samples.routing_around_barriers to javafx.fxml;
exports com.esri.samples.routing_around_barriers;
}