Find route
Display directions for a route between two points.
Use case
Find routes with driving directions between any number of locations. You might use the ArcGIS platform to create a custom network for routing on a private roads.
How to use the sample
For simplicity, the sample comes loaded with a start and end stop. You can tap on the "Find Route" button to display a route between these stops. Once the route is generated, turn-by-turn directions are shown in a list. You can use the "Reset" button to reset the sample.
How it works
- Create a
RouteTask
using a URL to an online route service. - Generate default
RouteParameters
usingrouteTask.createDefaultParametersAsync()
. - Use
setReturnDirections
on the parameters and set to true. - Use
setStops
on the parameters to addStop
s to its stops collection for each destination. - Solve the route using
routeTask.solveAsync(routeParameters)
to get aRouteResult
. - Iterate through the result's
Route
s. To display the route, create a graphic using the geometry fromroute.getRouteGeometry()
. To display directions, useroute.getDirectionManeuvers()
, and for eachDirectionManeuver
, displayDirectionManeuver.getDirectionText()
.
Relevant API
- DirectionManeuver
- Route
- RouteParameters
- RouteResult
- RouteTask
- Stop
Tags
directions, driving, navigation, network, network analysis, route, routing, shortest path, turn-by-turn
Sample Code
FindRouteSample.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright 2017 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.
*/
package com.esri.samples.find_route;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol.Style;
import com.esri.arcgisruntime.symbology.TextSymbol;
import com.esri.arcgisruntime.symbology.TextSymbol.HorizontalAlignment;
import com.esri.arcgisruntime.symbology.TextSymbol.VerticalAlignment;
import com.esri.arcgisruntime.tasks.networkanalysis.DirectionManeuver;
import com.esri.arcgisruntime.tasks.networkanalysis.Route;
import com.esri.arcgisruntime.tasks.networkanalysis.RouteParameters;
import com.esri.arcgisruntime.tasks.networkanalysis.RouteResult;
import com.esri.arcgisruntime.tasks.networkanalysis.RouteTask;
import com.esri.arcgisruntime.tasks.networkanalysis.Stop;
public class FindRouteSample extends Application {
private MapView mapView;
private RouteTask routeTask;
private RouteParameters routeParameters;
private final ListView<String> directionsList = new ListView<>();
private Graphic routeGraphic;
private final GraphicsOverlay routeGraphicsOverlay = new GraphicsOverlay();
private final SpatialReference ESPG_3857 = SpatialReference.create(102100);
private static final String ROUTE_TASK_SANDIEGO =
"https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/Route";
@Override
public void start(Stage stage) {
try {
// create stack pane and application scene
StackPane stackPane = new StackPane();
Scene scene = new Scene(stackPane);
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/find_route/style.css")).toExternalForm());
// set title, size, and add scene to stage
stage.setTitle("Find Route Sample");
stage.setWidth(800);
stage.setHeight(700);
stage.setScene(scene);
stage.show();
// authentication with an API key or named user is required to access basemaps and other location services
String yourAPIKey = System.getProperty("apiKey");
ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);
// create a control panel
VBox controlsVBox = new VBox(6);
controlsVBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.3)"), CornerRadii.EMPTY,
Insets.EMPTY)));
controlsVBox.setPadding(new Insets(10.0));
controlsVBox.setMaxSize(450, 300);
controlsVBox.getStyleClass().add("panel-region");
Label directionsLabel = new Label("Route directions:");
directionsLabel.getStyleClass().add("panel-label");
// create buttons for user interaction
Button findButton = new Button("Find route");
findButton.setMaxWidth(Double.MAX_VALUE);
findButton.setDisable(true);
Button resetButton = new Button("Reset");
resetButton.setMaxWidth(Double.MAX_VALUE);
resetButton.setDisable(true);
// find route
findButton.setOnAction(e -> {
try {
RouteResult result = routeTask.solveRouteAsync(routeParameters).get();
List<Route> routes = result.getRoutes();
if (routes.size() < 1) {
directionsList.getItems().add("No Routes");
}
Route route = routes.get(0);
Geometry shape = route.getRouteGeometry();
routeGraphic = new Graphic(shape, new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 2));
routeGraphicsOverlay.getGraphics().add(routeGraphic);
// get the direction text for each maneuver
for (DirectionManeuver step : route.getDirectionManeuvers()) {
directionsList.getItems().add(step.getDirectionText());
}
resetButton.setDisable(false);
findButton.setDisable(true);
} catch (Exception ex) {
ex.printStackTrace();
}
});
// clear the route and the directions maneuver found
resetButton.setOnAction(e -> {
routeGraphicsOverlay.getGraphics().remove(routeGraphic);
directionsList.getItems().clear();
resetButton.setDisable(true);
findButton.setDisable(false);
});
// add buttons and direction list and label to the control panel
controlsVBox.getChildren().addAll(directionsLabel, directionsList, findButton, resetButton);
// create a map with the streets basemap style
ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);
// create a map view and set the map to it
mapView = new MapView();
mapView.setMap(map);
// set the viewpoint to San Diego (U.S.)
mapView.setViewpointGeometryAsync(new Envelope(-13067866, 3843014, -13004499, 3871296, ESPG_3857));
// add the graphic overlay to the map view
mapView.getGraphicsOverlays().add(routeGraphicsOverlay);
try {
// create route task from San Diego service
routeTask = new RouteTask(ROUTE_TASK_SANDIEGO);
// load route task
routeTask.loadAsync();
routeTask.addDoneLoadingListener(() -> {
if (routeTask.getLoadStatus() == LoadStatus.LOADED) {
try {
// enable the find route button
findButton.setDisable(false);
// get default route parameters
routeParameters = routeTask.createDefaultParametersAsync().get();
routeParameters.setOutputSpatialReference(ESPG_3857);
// set flags to return stops and directions
routeParameters.setReturnStops(true);
routeParameters.setReturnDirections(true);
// set stop locations
Point stop1Loc = new Point(-1.3018598562659847E7, 3863191.8817135547, ESPG_3857);
Point stop2Loc = new Point(-1.3036911787723785E7, 3839935.706521739, ESPG_3857);
// add route stops
List<Stop> routeStops = new ArrayList<>();
routeStops.add(new Stop(stop1Loc));
routeStops.add(new Stop(stop2Loc));
routeParameters.setStops(routeStops);
// add route stops to the stops overlay
SimpleMarkerSymbol stopMarker = new SimpleMarkerSymbol(Style.CIRCLE, Color.BLUE, 14);
routeGraphicsOverlay.getGraphics().add(new Graphic(stop1Loc, stopMarker));
routeGraphicsOverlay.getGraphics().add(new Graphic(stop2Loc, stopMarker));
// add order text symbols to the stops
TextSymbol stop1Text = new TextSymbol(10, "1", Color.WHITE, HorizontalAlignment.CENTER, VerticalAlignment.MIDDLE);
TextSymbol stop2Text = new TextSymbol(10, "2", Color.WHITE, HorizontalAlignment.CENTER, VerticalAlignment.MIDDLE);
routeGraphicsOverlay.getGraphics().add(new Graphic(stop1Loc, stop1Text));
routeGraphicsOverlay.getGraphics().add(new Graphic(stop2Loc, stop2Text));
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
Alert alert = new Alert(Alert.AlertType.ERROR, "Route Task Failed to Load!");
alert.show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
// add the map view and control panel to stack pane
stackPane.getChildren().addAll(mapView, controlsVBox);
StackPane.setAlignment(controlsVBox, Pos.TOP_LEFT);
StackPane.setMargin(controlsVBox, new Insets(10, 0, 0, 10));
} catch (Exception e) {
// on any error, display the stack trace.
e.printStackTrace();
}
}
/**
* Stops and releases all resources used in application.
*/
@Override
public void stop() {
if (mapView != null) {
mapView.dispose();
}
}
/**
* Opens and runs application.
*
* @param args arguments passed to this application
*/
public static void main(String[] args) {
Application.launch(args);
}
}