A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.arcgis.com/javascript/latest/tutorials/find-a-route-and-directions/ below:

Find a route and directions | Overview | ArcGIS Maps SDK for JavaScript 4.33

Learn how to find a route and directions with the route service.

Routing is 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 several additional parameters, such as barriers and mode of travel, to refine the results.

In this tutorial, you will 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.

Note

You can also use the Directions component to find routes and directions between two or more locations. The component provides a user-friendly interface to find routes and directions. See the Display directions tutorial for more information on using the directions component.

To learn more about routing, visit Route and directions in the Mapping and location services guide.

Prerequisites ArcGIS Accounts:

You need an ArcGIS Location Platform or ArcGIS Online account.

Steps Create a new pen
  1. To get started, either complete the Display a map tutorial or .
Get an access token

You need an access token with the correct privileges to access the location services used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s):
  2. In CodePen, set esriConfig.apiKey to your access token.

    Use dark colors for code blocks

    1
    2
    3
    4
    5
    6
    7
    8
    9
      var esriConfig = {
        apiKey: "YOUR_ACCESS_TOKEN",
      };
    

To learn about other ways to get an access token, go to Types of authentication.

Update the map

A streets basemap layer is typically used in routing applications. Update the basemap attribute on the Map component to use the arcgis/navigation basemap layer and change the position of the map to center on Los Angeles.

  1. Update the basemap, center and zoom attributes.

    Expand

    Use dark colors for code blocks
    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
        <arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12">
    
          <arcgis-zoom position="top-left"></arcgis-zoom>
    
        </arcgis-map>
    
Add HTML to display directions
  1. Add an arcgis-placement with its position set to be the top right corner of the map.

    Expand

    Use dark colors for code blocks
    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
        <arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12">
    
          <arcgis-zoom position="top-left"></arcgis-zoom>
    
          <arcgis-placement position="top-right">
    
          </arcgis-placement>
    
        </arcgis-map>
    
  2. Inside the arcgis-placement add a calcite-panel.

    Expand

    Use dark colors for code blocks
    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
          <arcgis-placement position="top-right">
    
            <calcite-panel heading="Directions">
    
            </calcite-panel>
    
          </arcgis-placement>
    
  3. Inside the calcite-panel, add a calcite-notice that displays the instructions.

    Expand

    Use dark colors for code blocks
    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
          <arcgis-placement position="top-right">
    
            <calcite-panel heading="Directions">
    
              <calcite-notice id="directions-notice" icon="driving-distance" open>
                <div slot="message">
                  Click on the map in two different locations to solve the route.
                </div>
              </calcite-notice>
    
            </calcite-panel>
    
          </arcgis-placement>
    
  4. Add a div element where the directions will be displayed.

    Expand

    Use dark colors for code blocks
    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
          <arcgis-placement position="top-right">
    
            <calcite-panel heading="Directions">
    
              <calcite-notice id="directions-notice" icon="driving-distance" open>
                <div slot="message">
                  Click on the map in two different locations to solve the route.
                </div>
              </calcite-notice>
    
              <div id="directions-container"></div>
    
            </calcite-panel>
    
          </arcgis-placement>
    
  5. Add some CSS styling to set the size of the directions container div and calcite-notice.

    Expand

    Use dark colors for code blocks
    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
        <style>
          html,
          body {
            height: 100%;
            margin: 0;
          }
    
    
          #directions-container {
            max-height: 50vh;
            width: 25vw;
            overflow-y: auto;
          }
    
          #directions-notice {
            max-width: 25vw;
          }
    
        </style>
    
Add modules
  1. Use $arcgis.import to add the necessary modules in a new <script> at the bottom of the <body>. For more information, refer to the Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol and SimpleMarkerSymbol modules.

    The ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The $arcgis.import global function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK's different modules, visit the References page.

    Expand

    Use dark colors for code blocks
    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
        <script type="module">
    
          const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =
            await $arcgis.import([
              "@arcgis/core/Graphic.js",
              "@arcgis/core/rest/route.js",
              "@arcgis/core/rest/support/RouteParameters.js",
              "@arcgis/core/rest/support/FeatureSet.js",
              "@arcgis/core/symbols/SimpleLineSymbol.js",
              "@arcgis/core/symbols/SimpleMarkerSymbol.js",
            ]);
    
    
    
        </script>
    
Define the service url

The rest module makes a request to a service and returns the results. Use the route module to access the Routing service.

  1. Create a variable called routeUrl to reference the route service.

    Expand

    Use dark colors for code blocks
    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
        <script type="module">
    
          const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =
            await $arcgis.import([
              "@arcgis/core/Graphic.js",
              "@arcgis/core/rest/route.js",
              "@arcgis/core/rest/support/RouteParameters.js",
              "@arcgis/core/rest/support/FeatureSet.js",
              "@arcgis/core/symbols/SimpleLineSymbol.js",
              "@arcgis/core/symbols/SimpleMarkerSymbol.js",
            ]);
    
    
          const routeUrl =
            "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
    
    
        </script>
    
Get an origin and destination

A route module uses a stops parameter to find a route. Stops are graphics that represent the origin and destination locations for a route. Use an arcgisViewClick event listener on the arcgis-map to add graphics when clicking on the map. The graphics will define the stops for the route.

  1. Add a arcgisViewClick event listener to the map to add graphics to the view.

    Expand

    Use dark colors for code blocks
    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
        <script type="module">
    
          const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =
            await $arcgis.import([
              "@arcgis/core/Graphic.js",
              "@arcgis/core/rest/route.js",
              "@arcgis/core/rest/support/RouteParameters.js",
              "@arcgis/core/rest/support/FeatureSet.js",
              "@arcgis/core/symbols/SimpleLineSymbol.js",
              "@arcgis/core/symbols/SimpleMarkerSymbol.js",
            ]);
    
    
          const routeUrl =
            "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
    
          const viewElement = document.querySelector("arcgis-map");
          viewElement.addEventListener("arcgisViewClick", (event) => {
    
          });
    
    
        </script>
    
  2. Create an addGraphic function to display a white marker for the origin location and a black marker for the destination. Add the graphic to the map component's view.

    Expand

    Use dark colors for code blocks
    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
        <script type="module">
    
          const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =
            await $arcgis.import([
              "@arcgis/core/Graphic.js",
              "@arcgis/core/rest/route.js",
              "@arcgis/core/rest/support/RouteParameters.js",
              "@arcgis/core/rest/support/FeatureSet.js",
              "@arcgis/core/symbols/SimpleLineSymbol.js",
              "@arcgis/core/symbols/SimpleMarkerSymbol.js",
            ]);
    
    
          const routeUrl =
            "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
    
          const viewElement = document.querySelector("arcgis-map");
          viewElement.addEventListener("arcgisViewClick", (event) => {
    
          });
    
          function addGraphic(type, point) {
            const graphic = new Graphic({
              symbol: new SimpleMarkerSymbol({
                color: type === "origin" ? "white" : "black",
                size: "8px",
              }),
              geometry: point,
            });
            viewElement.view.graphics.add(graphic);
          }
    
    
        </script>
    
  3. Update the arcgisViewClick event listener to reference the addGraphic function. The first click will create the origin. The second will make the destination and hide the notice. Subsequent clicks will clear the graphics to define a new origin and destination.

    Expand

    Use dark colors for code blocks
    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
          const viewElement = document.querySelector("arcgis-map");
          viewElement.addEventListener("arcgisViewClick", (event) => {
    
            const directionsNotice = document.querySelector("#directions-notice");
            if (viewElement.view.graphics.length === 0) {
              addGraphic("origin", event.detail.mapPoint);
            } else if (viewElement.view.graphics.length === 1) {
              directionsNotice.open = false;
              addGraphic("destination", event.detail.mapPoint);
    
            } else {
              viewElement.view.graphics.removeAll();
              directionsNotice.open = true;
              document.querySelector("#directions-container").innerHTML = "";
              addGraphic("origin", event.detail.mapPoint);
            }
    
          });
    
  4. Click on the map repeatedly in different locations to ensure the graphics are created and the notice is only visible when there are less than two graphics.

Find the route

To solve the route, add the origin and destination graphics to the stops parameter as a FeatureSet and then use the solve method. The resulting route will be added to the map as a Graphic.

Input parameters are necessary to solve the route. While there are many parameters you can add, such as stops and barriers, at minimum, you need to provide an origin and destination point.

  1. Create a getRoute function to calculate the route and directions. Create an instance of RouteParameters and use the point graphics drawn on the map as the stops.

    Expand

    Use dark colors for code blocks
    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
          async function getRoute() {
            const routeParams = new RouteParameters({
              stops: new FeatureSet({
                features: viewElement.view.graphics.toArray(),
              }),
    
            });
    
          }
    
  2. Call the solve method to get the route. When the method returns, get the route from routeResults and add it to the view as a Graphic with a blue line.

    Expand

    Use dark colors for code blocks
    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
          async function getRoute() {
            const routeParams = new RouteParameters({
              stops: new FeatureSet({
                features: viewElement.view.graphics.toArray(),
              }),
    
            });
    
            try {
              const response = await route.solve(routeUrl, routeParams);
    
              response.routeResults.forEach((result) => {
                result.route.symbol = new SimpleLineSymbol({
                  color: [5, 150, 255],
                  width: 3,
                });
                viewElement.view.graphics.add(result.route);
              });
    
            } catch (error) {
              console.log(error);
            }
    
          }
    
  3. Update the arcgisViewClick event listener to call the getRoute function after creating the destination graphic.

    Expand

    Use dark colors for code blocks
    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
          const viewElement = document.querySelector("arcgis-map");
          viewElement.addEventListener("arcgisViewClick", (event) => {
    
            const directionsNotice = document.querySelector("#directions-notice");
            if (viewElement.view.graphics.length === 0) {
              addGraphic("origin", event.detail.mapPoint);
            } else if (viewElement.view.graphics.length === 1) {
              directionsNotice.open = false;
              addGraphic("destination", event.detail.mapPoint);
    
              getRoute();
    
            } else {
              viewElement.view.graphics.removeAll();
              directionsNotice.open = true;
              document.querySelector("#directions-container").innerHTML = "";
              addGraphic("origin", event.detail.mapPoint);
            }
    
          });
    
  4. Click two locations on the map to display a route.

Get directions

You can get driving directions from the route service with the returnDirections property on RouteParameters. Use this property to return directions and add them to the map as HTML elements.

  1. Go back to the getRoute function and set the returnDirections property to true.

    Expand

    Use dark colors for code blocks
    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
          async function getRoute() {
            const routeParams = new RouteParameters({
              stops: new FeatureSet({
                features: viewElement.view.graphics.toArray(),
              }),
    
              returnDirections: true,
    
            });
    
            try {
              const response = await route.solve(routeUrl, routeParams);
    
              response.routeResults.forEach((result) => {
                result.route.symbol = new SimpleLineSymbol({
                  color: [5, 150, 255],
                  width: 3,
                });
                viewElement.view.graphics.add(result.route);
              });
    
            } catch (error) {
              console.log(error);
            }
    
          }
    
  2. After the solve method has been resolved, and if there are results, create a calcite-list that will display the directions and append it to the div we created earlier with the id of directions-container.

    Expand

    Use dark colors for code blocks
    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
          async function getRoute() {
            const routeParams = new RouteParameters({
              stops: new FeatureSet({
                features: viewElement.view.graphics.toArray(),
              }),
    
              returnDirections: true,
    
            });
    
            try {
              const response = await route.solve(routeUrl, routeParams);
    
              response.routeResults.forEach((result) => {
                result.route.symbol = new SimpleLineSymbol({
                  color: [5, 150, 255],
                  width: 3,
                });
                viewElement.view.graphics.add(result.route);
              });
    
              if (response.routeResults.length > 0) {
                const directions = document.createElement("calcite-list");
    
                document.querySelector("#directions-container").appendChild(directions);
              }
    
            } catch (error) {
              console.log(error);
            }
    
          }
    
  3. Create a calcite-list-item for each route feature to generate an ordered list of directions with their distances in miles. Append each direction to the directions list.

    Expand

    Use dark colors for code blocks
    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
          async function getRoute() {
            const routeParams = new RouteParameters({
              stops: new FeatureSet({
                features: viewElement.view.graphics.toArray(),
              }),
    
              returnDirections: true,
    
            });
    
            try {
              const response = await route.solve(routeUrl, routeParams);
    
              response.routeResults.forEach((result) => {
                result.route.symbol = new SimpleLineSymbol({
                  color: [5, 150, 255],
                  width: 3,
                });
                viewElement.view.graphics.add(result.route);
              });
    
              if (response.routeResults.length > 0) {
                const directions = document.createElement("calcite-list");
    
                const features = response.routeResults[0].directions.features;
                features.forEach((feature, index) => {
                  const direction = document.createElement("calcite-list-item");
    
                  const step = document.createElement("span");
                  step.innerText = `${index + 1}.`;
                  step.slot = "content-start";
                  step.style.marginLeft = "10px";
                  direction.appendChild(step);
    
                  direction.label = `${feature.attributes.text}`;
                  direction.description = `${feature.attributes.length.toFixed(2)} miles`;
                  directions.appendChild(direction);
                });
    
                document.querySelector("#directions-container").appendChild(directions);
              }
    
            } catch (error) {
              console.log(error);
            }
    
          }
    
Run the App

In CodePen, run your code to display the map.

Click on the map twice to display the route directions. 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.

What's next?

Learn how to use additional API features and ArcGIS services in these tutorials:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4