Tutorial: Change the static basemap tiles style

Learn how to change the basemap style in a map using the static basemap tiles service (beta).

The static basemap tiles service (beta) provides access to raster basemap tiles for the world. The service supports a number of ArcGIS styles such as navigation, streets, outdoor, and light-gray. The tiles are returned as PNG files.

In this tutorial, you use the dropdown menu to toggle between the different static basemap tiles.

Prerequisites

This tutorial requires an ArcGIS Location Platform account.

Steps

Create a new pen

  1. Go to CodePen to create a new pen for your application.

Get an access token

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

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s):

    • Privileges
      • Location services > Basemaps
  2. Copy the API key access token to your clipboard when prompted.

  3. In CodePen, update the accessToken variable to use your access token.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    const accessToken = "YOUR_ACCESS_TOKEN";
    const basemapId = "arcgis/outdoor";
    const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`;
    olms.apply(map, basemapURL);
    
  4. Run the code to ensure the basemap is displayed in the map.

To learn about the other types of authentication available, go to Types of authentication.

Add a style selector

  1. Create a <div> tag to contain the <select> dropdown menu. Add the supported basemap style enumerations from the static basemap tiles service (beta) as <option>s.

    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
      <div id="map"></div>
    
      <div id="basemaps-wrapper">
    
        <select id="basemaps">
          <option value="beta/arcgis/outdoor">arcgis/outdoor</option>
          <option value="beta/arcgis/streets">arcgis/streets</option>
          <option value="beta/arcgis/streets-night">arcgis/streets-night</option>
          <option value="beta/arcgis/navigation">arcgis/navigation</option>
          <option value="beta/arcgis/navigation-night">arcgis/navigation-night</option>
          <option value="beta/arcgis/light-gray">arcgis/light-gray</option>
          <option value="beta/arcgis/dark-gray">arcgis/dark-gray</option>
          <option value="beta/arcgis/imagery/labels">arcgis/imagery</option>
          <option value="beta/arcgis/oceans/labels">arcgis/oceans</option>
          <option value="beta/arcgis/community">arcgis/community</option>
          <option value="beta/arcgis/nova">arcgis/nova</option>
          <option value="beta/arcgis/midcentury">arcgis/midcentury</option>
          <option value="beta/arcgis/newspaper">arcgis/newspaper</option>
          <option value="beta/arcgis/human-geography">arcgis/human-geography</option>
          <option value="beta/arcgis/human-geography-dark">arcgis/human-geography-dark</option>
        </select>
    
      </div>
    
      <script>
    
      </script>
    
  2. In the <style> element, add CSS styling to the basemaps-wrapper and basemaps.

    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
        html,
        body,
        #map {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
          font-family: Arial, Helvetica, sans-serif;
          font-size: 14px;
          color: #323232;
        }
    
    
        #basemaps-wrapper {
          position: absolute;
          top: 20px;
          right: 20px;
          background: rgba(255, 255, 255, 0);
        }
    
        #basemaps {
          font-size: 16px;
          padding: 4px 8px;
        }
    
    

Update the map's viewpoint

  1. Change the map's center to [-91.2996, 37.1174] and zoom level to 5. This will focus the map on the United States of America.

    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
        const map = new ol.Map({
          target: "map",
    
          view: new ol.View({
            center: ol.proj.fromLonLat([-91.2996, 37.1174]), // USA (x, y)
            zoom: 5,
          }),
    
        });
    

Set the basemap

Use a function to reference the static basemap tiles service (beta) and a style enumeration to update the map. This will be used when a selection is made.

  1. Change the basemapId to beta/arcgis/outdoor. This will be the default basemap.

    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
        const basemapId = "beta/arcgis/outdoor";
    
    Expand
  2. Change the value of basemapURL to the static basemap tiles service (beta) endpoint. Then, create a url variable that will append the name of the basemap selected from the dropdown menu.

    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
        const accessToken = "YOUR_ACCESS_TOKEN"
    
        const basemapId = "beta/arcgis/outdoor";
    
        const basemapURL = "https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service";
    
        const url = (style) => `${basemapURL}/${style}/static/tile/{z}/{y}/{x}?token=${accessToken}`;
    
        const map = new ol.Map({
          target: "map",
    
          view: new ol.View({
            center: ol.proj.fromLonLat([-91.2996, 37.1174]), // USA (x, y)
            zoom: 5,
          }),
    
        });
    
    
  3. Create a Tile object that will provide basemap tiles to the map. Pass basemapId into the tile source's URL.

    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
        const accessToken = "YOUR_ACCESS_TOKEN"
    
        const basemapId = "beta/arcgis/outdoor";
    
        const basemapURL = "https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service";
    
        const url = (style) => `${basemapURL}/${style}/static/tile/{z}/{y}/{x}?token=${accessToken}`;
    
        const tileLayer = new ol.layer.Tile({
          source: new ol.source.XYZ({
            url: url(basemapId), // Default basemap
            tileSize: 512,
          }),
        });
    
        const map = new ol.Map({
          target: "map",
    
          view: new ol.View({
            center: ol.proj.fromLonLat([-91.2996, 37.1174]), // USA (x, y)
            zoom: 5,
          }),
    
        });
    
    
  4. Pass the tileLayer into the map's layers attribute.

    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
        const map = new ol.Map({
          target: "map",
    
          layers: [tileLayer],
    
          view: new ol.View({
            center: ol.proj.fromLonLat([-91.2996, 37.1174]), // USA (x, y)
            zoom: 5,
          }),
    
        });
    
    Expand
  5. Run the code to ensure the map displays the default basemap and that the <select> element contains different basemap enumerations.

Add an event listener

Use an event listener to register a basemap change in the <select> element and to update the map.

  1. Create a basemapsSelectElement to return the basemap from the selector.

    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
        const basemapsSelectElement = document.querySelector("#basemaps");
    
    
    Expand
  2. Add an event listener to store the selected basemap.

    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
        const basemapsSelectElement = document.querySelector("#basemaps");
    
        basemapsSelectElement.addEventListener("change", (e) => {
          const style = e.target.value;
    
        });
    
    Expand
  3. Update the tileLayer's source URL with the new basemap style.

    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
        const basemapsSelectElement = document.querySelector("#basemaps");
    
        basemapsSelectElement.addEventListener("change", (e) => {
          const style = e.target.value;
    
          tileLayer.getSource().setUrl(url(style));
    
        });
    
    Expand

Update attribution

You will update the attribution text for basemap styles service from the previous tutorial to the attribution for static basemap tiles service (beta). You retrieve the data attribution manually from the service URL and display it in the map's attribution control. You also prepend Powered by Esri in the attribution string.

  1. Fetch the copyrightText from the service URL. Then, display it and prepend Powered by Esri string in the map's attribution control.

    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
        // Add Esri and data attribution
        // Learn more in https://esriurl.com/attribution
        fetch(`${basemapURL}/${basemapId}/static?token=${accessToken}`)
          .then(response => response.json())
          .then(data => {
            tileLayer.getSource().setAttributions(["Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a> | " + data.copyrightText]);
        });
    
    Expand

Run the app

In CodePen, run your code to display the map.

You should be able to use the select element to switch between static basemap tiles.

What's next?

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