Skip to content

Change the basemap style

Learn how to change a basemap style in a map.

Change the basemap style using API key authentication

The ArcGIS Basemap Styles service provides a number of basemap styles such as topography, streets, outdoor, navigation, and imagery that you can use in maps. To access these basemap styles in your MapLibre GL JS app, use the MapLibre ArcGIS plugin.

In this tutorial, use a <select> dropdown menu to toggle between different basemap styles.

Prerequisites

You need an ArcGIS Location Platform or ArcGIS Online account.

Steps

Get the starter app

Select a type of authentication below and follow the steps to create a new application.

You can choose one of the following to create a new CodePen:

  • Option 1: Complete the Display a map tutorial; or,
  • Option 2: Start from the Display a map tutorial .

Set up authentication

Create developer credentials in your portal for the type of authentication you selected.

Create a new API key credential with the correct privileges to access the resources used in this tutorial.

  1. Go to the Create an API key tutorial to follow the steps to get an access token with these privilege(s):
    • Privileges
      • Location services > Basemaps

Set developer credentials

Use the API key or OAuth developer credentials so your application can access ArcGIS services.

  1. Update the accessToken variable to use your API key.

    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
        /* Use for API key authentication */
        const accessToken = "YOUR_ACCESS_TOKEN";
    
    Expand

Add a style selector

  1. In the wrapper element, add a <select> element, which you will populate in a later step.

    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
      </style>
      <!-- Load MapLibre GL JS from CDN -->
      <script src=https://unpkg.com/maplibre-gl@5.8.0/dist/maplibre-gl.js></script>
      <link href=https://unpkg.com/maplibre-gl@5.8.0/dist/maplibre-gl.css rel="stylesheet" />
      <!-- Load MapLibre ArcGIS from CDN -->
      <script src="https://unpkg.com/@esri/maplibre-arcgis@1.0.0/dist/umd/maplibre-arcgis.min.js"></script>
    
    </head>
    
    <body>
      <div id="map"></div>
    
      <div id="basemaps-wrapper">
    
        <select id="basemaps"></select>
    
      </div>
    
    Expand
  2. In the <style> element, add CSS styling to the basemaps-wrapper and basemaps.

    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
      <style>
        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;
        }
    
    
      </style>
      <!-- Load MapLibre GL JS from CDN -->
      <script src=https://unpkg.com/maplibre-gl@5.8.0/dist/maplibre-gl.js></script>
      <link href=https://unpkg.com/maplibre-gl@5.8.0/dist/maplibre-gl.css rel="stylesheet" />
      <!-- Load MapLibre ArcGIS from CDN -->
      <script src="https://unpkg.com/@esri/maplibre-arcgis@1.0.0/dist/umd/maplibre-arcgis.min.js"></script>
    
    Expand

Set the basemap

Using the MapLibre ArcGIS plugin, reference the Basemap Styles service and a style enumeration to update the map. This will be used when a selection is made.

  1. Comment out the map style as it will be updated dynamically based on the user's selection.

    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
        // const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, {
        //   style: 'arcgis/outdoor',
        //   token: accessToken
        // });
    
    Expand
  2. Define a function to update the map style using the selected basemap layer.

    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
        const setBasemap = (selection) => {
          const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, {
            style: selection,
            token: accessToken
          });
        };
    
    Expand
  3. Select the DOM element used for the basemap 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
        const basemapsSelectElement = document.querySelector("#basemaps");
    
    Expand
  4. Use the MapLibre ArcGIS plugin to make a request to the Basemap Styles service. This dynamically fetches the available basemap styles and populates the select element.

    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
        maplibreArcGIS.BasemapStyle.getSelf().then(response => {
          const basemapStyles = response.styles.filter(style => !style.deprecated)
          basemapStyles.forEach(style => {
            const option = document.createElement("option");
            option.value = style.path;
            option.textContent = style.name;
            basemapsSelectElement.appendChild(option);
          });
    
        })
    
    Expand
  5. Initialize the basemap layer with the first style returned from the response. This ensures the map loads with a default style when the page first loads.

    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
        maplibreArcGIS.BasemapStyle.getSelf().then(response => {
          const basemapStyles = response.styles.filter(style => !style.deprecated)
          basemapStyles.forEach(style => {
            const option = document.createElement("option");
            option.value = style.path;
            option.textContent = style.name;
            basemapsSelectElement.appendChild(option);
          });
    
          setBasemap(response.styles[0].path);
    
        })
    
    Expand
  6. Run the code to ensure that the <select> element contains different basemap enumerations.

Add an event listener

Call setBasemap when the user selects an option in the <select> menu.

  1. Add an event listener to update the map to the new basemap when the selector is changed.

    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
        const basemapsSelectElement = document.querySelector("#basemaps");
    
        maplibreArcGIS.BasemapStyle.getSelf().then(response => {
          const basemapStyles = response.styles.filter(style => !style.deprecated)
          basemapStyles.forEach(style => {
            const option = document.createElement("option");
            option.value = style.path;
            option.textContent = style.name;
            basemapsSelectElement.appendChild(option);
          });
    
          setBasemap(response.styles[0].path);
    
        })
    
        basemapsSelectElement.addEventListener("change", (e) => {
          setBasemap(e.target.value);
        });
    
    Expand

Run the app

Run the app.

Try selecting different basemap styles from the selector. As you select the different styles, you should see the basemap change accordingly.

What's next?

Learn how to use additional location services in these tutorials:

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