Find a route

View on GitHubSample viewer app

Display directions for a route between two points.

screenshot

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 click on the Find Route to display a route between these stops. Once the route is generated, turn-by-turn directions are shown in a list.

How it works

  1. Create a RouteTask using a URL to an online route service.
  2. Generate default RouteParameters using routeTask.createDefaultParameters().
  3. Set returnStops and returnDirections on the parameters to true.
  4. Add Stops to the parameters stops collection for each destination.
  5. Solve the route using routeTask.solveRoute(routeParameters) to get a RouteResult.
  6. Iterate through the result's Routes. To display the route, create a graphic using the geometry from route.routeGeometry(). To display directions, use route.directionManeuvers() and apply the list model to the UI.

Relevant API

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

Tags

directions, driving, navigation, network, network analysis, route, routing, shortest path, turn-by-turn

Sample Code

FindRoute.qml
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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// [WriteFile Name=FindRoute, Category=Routing]
// [Legal]
// Copyright 2016 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.
// [Legal]

import QtQuick 2.6
import QtQuick.Controls 2.2
import Esri.ArcGISRuntime 100.15

Rectangle {
    id: mainRect
    width: 800
    height: 600

    property Point stop1Geometry: null
    property Point stop2Geometry: null
    property var routeParameters: null
    property var directionListModel: null

    // Create window for displaying the route directions
    Rectangle {
        id: directionWindow
        anchors {
            right: parent.right
            top: parent.top
            bottom: parent.bottom
        }
        visible: false
        width: Qt.platform.os === "ios" || Qt.platform.os === "android" ? 250 : 350
        color: "#FBFBFB"

        //! [FindRoute qml ListView directionsView]
        ListView {
            id: directionsView
            anchors {
                fill: parent
                margins: 5
            }
            header: Component {
                Text {
                    height: 40
                    text: "Directions:"
                    font.pixelSize: 22
                }
            }

            // set the model to the DirectionManeuverListModel returned from the route
            model: directionListModel
            delegate: directionDelegate
        }
        //! [FindRoute qml ListView directionsView]
    }

    // Create MapView that contains a Map with the Topographic Basemap
    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        // set the transform to animate showing the direction window
        transform: Translate {
            id: translate
            x: 0
            Behavior on x { NumberAnimation { duration: 300; easing.type: Easing.OutQuad } }
        }

        // Create a GraphicsOverlay to display the route
        GraphicsOverlay {
            id: routeGraphicsOverlay

            // Set the renderer
            SimpleRenderer {
                SimpleLineSymbol {
                    color: "cyan"
                    style: Enums.SimpleLineSymbolStyleSolid
                    width: 4
                }
            }
        }

        // Create a GraphicsOverlay to display the stops
        GraphicsOverlay { id: stopsGraphicsOverlay }

        // Create a map with a basemap and initial viewpoint
        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISNavigation
            }

            initialViewpoint: ViewpointCenter {
                Point {
                    x: -13041154
                    y: 3858170
                    spatialReference: SpatialReference { wkid: 3857 }
                }
                targetScale: 1e5
            }

            // Add the graphics and setup the RouteTask once the map is loaded
            onLoadStatusChanged: {
                addStopGraphics();
                setupRouteTask();
            }
        }

        // Create the solve button to solve the route
        Rectangle {
            id: solveButton
            property bool pressed: false
            anchors {
                horizontalCenter: parent.horizontalCenter
                bottom: mapView.attributionTop
                bottomMargin: 5
            }

            width: 130
            height: 30
            color: pressed ? "#959595" : "#D6D6D6"
            radius: 5
            border {
                color: "#585858"
                width: 1
            }

            Text {
                id: routeButtonText
                anchors.centerIn: parent
                text: "Solve route"
                font.pixelSize: 14
                color: "#35352E"
            }

            MouseArea {
                anchors.fill: parent
                onPressed: solveButton.pressed = true
                onReleased: solveButton.pressed = false
                onClicked: {
                    if (routeParameters !== null) {
                        // set parameters to return directions
                        routeParameters.returnDirections = true;

                        // clear previous route graphics
                        routeGraphicsOverlay.graphics.clear();

                        // clear previous stops from the parameters
                        routeParameters.clearStops();

                        // set the stops to the parameters
                        const stop1 = ArcGISRuntimeEnvironment.createObject("Stop", {geometry: stop1Geometry, name: "Origin"});
                        const stop2 = ArcGISRuntimeEnvironment.createObject("Stop", {geometry: stop2Geometry, name: "Destination"});
                        routeParameters.setStops([stop1, stop2]);

                        // solve the route with the parameters
                        routeTask.solveRoute(routeParameters);
                    }
                }
            }
        }

        // Create a button to show the direction window
        Rectangle {
            id: directionButton

            property bool pressed: false

            visible: !solveButton.visible
            anchors {
                right: parent.right
                bottom: parent.bottom
                rightMargin: 10
                bottomMargin: 40
            }

            width: 45
            height: width
            color: pressed ? "#959595" : "#D6D6D6"
            radius: 100
            border {
                color: "#585858"
                width: 1.5
            }

            Image {
                anchors.centerIn: parent
                width: 35
                height: width
                source: "qrc:/Samples/Routing/FindRoute/directions.png"
            }

            MouseArea {
                anchors.fill: parent
                onPressed: directionButton.pressed = true
                onReleased: directionButton.pressed = false
                onClicked: {
                    // Show the direction window when it is clicked
                    translate.x = directionWindow.visible ? 0 : (directionWindow.width * -1);
                    directionWindow.visible = !directionWindow.visible;
                }
            }
        }
    }

    //! [FindRoute RouteTask]

    // Create a RouteTask pointing to an online service
    RouteTask {
        id: routeTask
        url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/Route"

        // Request default parameters once the task is loaded
        onLoadStatusChanged: {
            if (loadStatus === Enums.LoadStatusLoaded) {
                routeTask.createDefaultParameters();
            }
        }

        // Store the resulting route parameters
        onCreateDefaultParametersStatusChanged: {
            if (createDefaultParametersStatus === Enums.TaskStatusCompleted) {
                routeParameters = createDefaultParametersResult;
            }
        }

        // Handle the solveRouteStatusChanged signal
        onSolveRouteStatusChanged: {
            if (solveRouteStatus === Enums.TaskStatusCompleted) {
                // Add the route graphic once the solve completes
                const generatedRoute = solveRouteResult.routes[0];
                const routeGraphic = ArcGISRuntimeEnvironment.createObject("Graphic", {geometry: generatedRoute.routeGeometry});
                routeGraphicsOverlay.graphics.append(routeGraphic);

                // set the direction maneuver list model
                directionListModel = generatedRoute.directionManeuvers;

                // hide the solve button and show the direction button
                solveButton.visible = false;
            }
        }
    }
    //! [FindRoute RouteTask]

    Component {
        id: directionDelegate
        Rectangle {
            id: rect
            width: parent.width
            height: 35
            color: directionWindow.color

            Rectangle {
                anchors {
                    top: parent.top;
                    left: parent.left;
                    right: parent.right;
                    topMargin: -8
                    leftMargin: 20
                    rightMargin: 20
                }
                color: "darkgrey"
                height: 1
            }

            Text {
                text: directionText
                anchors {
                    fill: parent
                    leftMargin: 5
                }
                elide: Text.ElideRight
                font.pixelSize: 14
            }
        }
    }

    function addStopGraphics() {
        //! [FindRoute qml addStopGraphics]
        // create the stop graphics' geometry
        stop1Geometry = ArcGISRuntimeEnvironment.createObject("Point", {
                                                                  x: -13041171,
                                                                  y: 3860988,
                                                                  spatialReference: Factory.SpatialReference.createWebMercator()
                                                              });
        stop2Geometry = ArcGISRuntimeEnvironment.createObject("Point", {
                                                                  x: -13041693,
                                                                  y: 3856006,
                                                                  spatialReference: Factory.SpatialReference.createWebMercator()
                                                              });

        // create the stop graphics' symbols
        const stop1Symbol = ArcGISRuntimeEnvironment.createObject("PictureMarkerSymbol", {
                                                                      url: "qrc:/Samples/Routing/FindRoute/pinA.png",
                                                                      width: 32,
                                                                      height: 32,
                                                                      offsetY: 16
                                                                  });
        const stop2Symbol = ArcGISRuntimeEnvironment.createObject("PictureMarkerSymbol", {
                                                                      url: "qrc:/Samples/Routing/FindRoute/pinB.png",
                                                                      width: 32,
                                                                      height: 32,
                                                                      offsetY: 16
                                                                  });

        // create the stop graphics
        const stop1Graphic = ArcGISRuntimeEnvironment.createObject("Graphic", {geometry: stop1Geometry, symbol: stop1Symbol});
        const stop2Graphic = ArcGISRuntimeEnvironment.createObject("Graphic", {geometry: stop2Geometry, symbol: stop2Symbol});

        // add to the overlay
        stopsGraphicsOverlay.graphics.append(stop1Graphic);
        stopsGraphicsOverlay.graphics.append(stop2Graphic);
        //! [FindRoute qml addStopGraphics]
    }

    function setupRouteTask() {
        // load the RouteTask
        routeTask.load();
    }
}

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