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/change-the-basemap-style/ below:

Change the basemap style | Overview | ArcGIS Maps SDK for JavaScript 4.33

Learn how to change the basemap style in a map using the Basemap styles service.

The basemap styles service provides a number of ArcGIS basemap layer styles such as topography, navigation, outdoors, and imagery that you can use in maps.

In this tutorial, you will use a Calcite Combobox to select and display different basemap styles.

Note

For more information, visit Basemap layers 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.

Add modules

The request module can be used to request data from a remote server or file. We'll use this module to fetch the available styles from the basemap style API endpoint.

  1. In a new <script> at the bottom of the <body>, use $arcgis.import() to add the request module.

    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
        <script type="module">
          const esriRequest = await $arcgis.import("@arcgis/core/request.js");
    
        </script>
    
Create a style dropdown

Get the list of styles available from the basemap style service and add them to a calcite-combobox component to allow users to change the style of their basemap.

  1. Add an arcgis-placement component to the map to place the content in the top-right of the map component.

    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
        <arcgis-map basemap="arcgis/topographic" center="-118.805, 34.027" zoom="13">
          <arcgis-zoom position="top-left"></arcgis-zoom>
    
          <arcgis-placement position="top-right">
    
          </arcgis-placement>
    
        </arcgis-map>
    
  2. Inside the arcgis-placement component, create a div called basemapStyles with a calcite-combobox component. Set selection-mode to single, so only one style can be selected at a time. Add the clear-disabled attribute so that a style is always selected.

    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
        <arcgis-map basemap="arcgis/topographic" center="-118.805, 34.027" zoom="13">
          <arcgis-zoom position="top-left"></arcgis-zoom>
    
          <arcgis-placement position="top-right">
    
            <div id="basemapStyles">
              <calcite-label>
                Basemap Style:
                <calcite-combobox
                  id="styleCombobox"
                  selection-mode="single"
                  clear-disabled></calcite-combobox>
              </calcite-label>
            </div>
    
          </arcgis-placement>
    
        </arcgis-map>
    
  3. Within the <style> tags, apply some CSS properties to the basemapStyles div.

    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
          #basemapStyles {
            background-color: white;
            border-radius: 5px;
            padding: 10px;
            width: 250px;
          }
    
  4. Create a variable for the Map component and the combobox and wait for the view to be ready.

    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
          const viewElement = document.querySelector("arcgis-map");
          const styleCombobox = document.querySelector("#styleCombobox");
          // wait for the view to be ready
          await viewElement.viewOnReady();
    
    
  5. Fetch the list of styles available from the /self endpoint for the basemap styles service using the esriRequest method. Specify json as the response type. This will return a json response with a styles property.

    REST API:

    To view the available styles, see Basemap styles -> Requests in the API reference.

    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
          // update combobox values with styles from rest endpoint
          const basemapStylesEndpoint =
            "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self";
          esriRequest(basemapStylesEndpoint, {
            responseType: "json",
          }).then((response) => {
            const json = response.data;
            // add each style as a combobox item
            json.styles.forEach((style) => {
    
            });
          });
    
  6. From the response data, use the JavaScript forEach method to iterate through each of the styles and add each style as a calcite-combobox-item. Set the value to the style path, and the label to the style name. If the style path matches the current basemap, set the item as selected.

    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
          // update combobox values with styles from rest endpoint
          const basemapStylesEndpoint =
            "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self";
          esriRequest(basemapStylesEndpoint, {
            responseType: "json",
          }).then((response) => {
            const json = response.data;
            // add each style as a combobox item
            json.styles.forEach((style) => {
    
              // if the style is complete and not deprecated, add it to the combobox
              if (style.complete && !style.deprecated) {
                const comboboxItem = document.createElement("calcite-combobox-item");
                comboboxItem.value = style.path;
                comboboxItem.heading = style.name;
                // if the current basemap style is the same as the combobox item, select it
                comboboxItem.selected = viewElement.basemap.style.id === style.path;
                styleCombobox.appendChild(comboboxItem);
              }
    
            });
          });
    
Update the style on user input

Use the calciteComboboxChange event to recognize when the user has selected a new language.

  1. Add an event listener to watch for changes on the combobox. When the combobox value has changed, update the basemap style to the selected item.

    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
          styleCombobox.addEventListener("calciteComboboxChange", (event) => {
            const basemapId = event.target.value;
            if (basemapId) {
              // update the basemap style
              viewElement.basemap = basemapId;
            }
          });
    
Note

The JavaScript Maps SDK also provides two out of the box components for changing the basemap:

Run the App

In CodePen, run your code to display the map.

Use the Calcite combobox in the top right to select and explore different basemap styles from the Basemap styles service (v2).

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