Learn how to find a route and directions with the route service.
Routing is the process of finding the path from an origin to a destination in a street network. You can use the Routing service to find routes, get driving directions, calculate drive times, and solve complicated, multiple vehicle routing problems. To create a route, you typically define a set of stops (origin and one or more destinations) and use the service to find a route with directions. You can also use a number of additional parameters such as barriers and mode of travel to refine the results.
In this tutorial, you define an origin and destination by clicking on the map. These values are used to get a route and directions from the route service. The directions are also displayed on the map.
Prerequisites
The following are required for this tutorial:
An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
To start this tutorial, complete the Display a map tutorial, or download and unzip the Display a map solution into a new folder.
Open the build.gradle file as a project in IntelliJ IDEA.
If you downloaded the solution project, set your API key.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
Go to your developer dashboard to get your API key.
For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
In the IntelliJ IDEA > Project tool window, open src/main/java/com.example.app and click App.
In the start() method, set the API key property on the ArcGISRuntimeEnvironment with your API key. Replace YOUR_API_KEY with your actual API Key. Be sure to surround your API Key with quotes, because the parameter passed to setApiKey is a string.
App.java
Use dark colors for code blocks
Change lineChange lineChange lineChange line
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
@Overridepublicvoidstart(Stage stage){
// set the title and size of the stage and show it stage.setTitle("Display a map tutorial");
stage.setWidth(800);
stage.setHeight(700);
stage.show();
// create a JavaFX scene with a stack pane as the root node, and add it to the scene StackPane stackPane = new StackPane();
Scene scene = new Scene(stackPane);
stage.setScene(scene);
// Note: it is not best practice to store API keys in source code.// The API key is referenced here for the convenience of this tutorial. String yourApiKey = "YOUR_API_KEY";
ArcGISRuntimeEnvironment.setApiKey(yourApiKey);
Add import statements and variable declarations
Add import statements and variable declarations to reference the packages and classes required for this tutorial.
In the IntelliJ IDEA > Project tool window, open src/main/java/com.example.app and click App.
Add the following imports above the existing imports:
Within the App class, add the following member variables to easily reference them from other parts of the application:
Member variables ensure that objects are not deallocated while asynchronous methods are executing. If an object is deallocated while one of its asynchronous methods is executing, the completion callback closure will never be called.
A streets basemap layer is typically used in routing applications. Update the basemap to use the ARCGIS_STREETS basemap style, and change the position of the map to center on Los Angeles.
Update the BasemapStyle from ARCGIS_TOPOGRAPHIC to ARCGIS_STREETS.
Update the latitude and longitude coordinates to center on Los Angeles.
To display the turn-by-turn directions from the route, a UI element is required.
In the start() method, set the maximum size of the directionsList (a JavaFX ListView which will display a vertical list of directions), and then add it to the stackPane.
A graphics overlay is a container for graphics. Graphics will be added later in this tutorial as a visual means to display the route stops and route result on the map.
A task makes a request to a service and returns the results. Use the RouteTask class to access a routing service.A routing service with global coverage is part of ArcGIS location services. You can also publish custom routing services using ArcGIS Enterprise.
Get default RouteParameters objects from the routeTask and set the return directions property to true. This specifies that the route results should include turn-by-turn directions. Add a UI text prompt.
A RouteTask requires at least a single origin and destination stop to find a route. Use a click handler on the mapView to add stops and display them as graphics when the map is clicked.
When a user clicks on the map, a stop will be added to a list of route stops. In this tutorial, the first click will create the origin stop and the second will create the destination stop.
Create a new addStopsOnMouseClicked() method which adds a setOnMouseClicked() listener to the mapView. Convert the clicked JavaFX Point2D to a location Point. Use the point to create a new Stop and add it to the routeStops list.
To display the stops on the map as they are added, add a listener to the routeStopsObservableList and create a new SimpleMarkerSymbol for each Stop. Use the stop's geometry to create a new Graphic and add it to the graphicsOverlay.
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
addStopsOnMouseClicked();
routeStops.addListener((ListChangeListener<Stop>) e -> {
// tracks the number of stops added to the map, and use it to create graphic geometry and symbol textint routeStopsSize = routeStops.size();
// handle user interactionif (routeStopsSize == 0) {
return;
} elseif (routeStopsSize == 1) {
graphicsOverlay.getGraphics().clear();
if (!directionsList.getItems().isEmpty())
directionsList.getItems().clear();
directionsList.getItems().add("Click to add two points to the map to find a route.");
}
// create a blue circle symbol for the stop SimpleMarkerSymbol stopMarker = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xff0000ff, 20);
// get the stop's geometry Geometry routeStopGeometry = routeStops.get(routeStopsSize-1).getGeometry();
graphicsOverlay.getGraphics().add(new Graphic(routeStopGeometry, stopMarker));
});
Expand
Find and display the route
To find the route between the origin and destination, set the stops on the route parameters and solve the route using the RouteTask. Display the route result on the map as a graphic.
When an origin and destination stop have been created, set them on the routeParameters.
Solve (find) the route based on the provided route parameters to obtain a RouteResult. The result is a collection of computed routes. Then, get the list of Route objects from the result. Each element represents an independent route with its own driving directions.The routing service only returns the optimal route.
The solveRouteAsync() method is asynchronous and you must handle its completion, check for errors, and confirm that a populated list of Route objects has been returned.
Check that a route was returned, and retrieve the first one. Display this route to the map by creating a new Graphic using the route's Geometry and a SimpleLineSymbol, and add it to the graphicsOverlay.
Run the app. Ensure to run the app as a Gradle task and not as an application in your IDE. In the Gradle tool window, under Tasks > application, click run.
The map should support two clicks to create an origin and destination point and then use the route service to display the resulting route and turn-by-turn directions.