Change the basemap layer

Learn how to change the basemap layer in a map.

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

In this tutorial, you use a <select> dropdown menu with ol-mapbox-style to toggle between a number of different basemap layer styles.

Prerequisites

You need a free ArcGIS developer account to access your dashboard and API keys. The API key must be scoped to access the services used in this tutorial.

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

Set the API key

To access ArcGIS location services, you need an API key.

  1. Go to your dashboard to get an API key.

  2. In CodePen, update apiKey to use your key.

    Change line
        
    const apiKey = "YOUR-API-KEY";
    const basemapId = "ArcGIS:Streets";
    const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey;
    olms(map, basemapURL);
    

Remove basemap references

  1. Remove the basemapId, basemapURL, and the olms function.

    Remove lineRemove lineRemove line
                                                        
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Display a map</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
    
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js"></script>
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
    
          const map = new ol.Map({ target: "map" });
    
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const apiKey = "YOUR-API-KEY";
    
          const basemapId = "ArcGIS:Streets";
    
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey;
    
          olms(map, basemapURL);
    
        </script>
    
      </body>
    </html>

Add the Select Menu

OpenLayers does not have a basemap layer switching widget, so you will use the a plain HTML <select> element.

This tutorial is inspired by [Esri Leaflet: Change the basemap layer](xref://site.esri-leaflet/change-the-basemap-layer).
  1. In the <head> tag, add CSS that will position the <select> menu wrapper element in the upper right corner and provide styling.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                    
    
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Change the basemap layer</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <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;
          }
          #basemaps {
            padding: 4px 8px;
            font-size: 16px;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <div id="basemaps-wrapper">
    
          <select id="basemaps">
            <option value="ArcGIS:Streets">Streets</option>
            <option value="ArcGIS:Navigation">Navigation</option>
            <option value="ArcGIS:Topographic">Topographic</option>
            <option value="ArcGIS:LightGray">Light Gray</option>
            <option value="ArcGIS:DarkGray">Dark gray</option>
            <option value="ArcGIS:StreetsRelief">Streets Relief</option>
            <option value="ArcGIS:Imagery:Standard">Imagery</option>
            <option value="ArcGIS:ChartedTerritory">ChartedTerritory</option>
            <option value="ArcGIS:ColoredPencil">ColoredPencil</option>
            <option value="ArcGIS:Nova">Nova</option>
            <option value="ArcGIS:Midcentury">Midcentury</option>
            <option value="OSM:Standard">OSM</option>
            <option value="OSM:Streets">OSM:Streets</option>
          </select>
    
        </div>
    
        <script>
          const apiKey = "YOUR-API-KEY";
          const map = new ol.Map({ target: "map" });
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles";
          const url = (name) => `${baseUrl}/${name}?type=style&apiKey=${apiKey}`;
    
          const setBasemap = (name) => {
    
            // Clear out existing layers.
            map.setLayerGroup(new ol.layer.Group());
    
            // Instantiate the given basemap layer.
            olms(map, url(name));
          };
    
          setBasemap('ArcGIS:Streets');
    
          const basemapsSelectElement = document.querySelector('#basemaps');
    
          basemapsSelectElement.addEventListener('change', (e) => {
            setBasemap(e.target.value);
          });
    
        </script>
    
      </body>
    </html>
  2. In the <body> element, add the wrapper tag with an id of basemaps-wrapper.

    Add line.Add line.Add line.
                                                                                                    
    
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Change the basemap layer</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <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;
          }
          #basemaps {
            padding: 4px 8px;
            font-size: 16px;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <div id="basemaps-wrapper">
    
          <select id="basemaps">
            <option value="ArcGIS:Streets">Streets</option>
            <option value="ArcGIS:Navigation">Navigation</option>
            <option value="ArcGIS:Topographic">Topographic</option>
            <option value="ArcGIS:LightGray">Light Gray</option>
            <option value="ArcGIS:DarkGray">Dark gray</option>
            <option value="ArcGIS:StreetsRelief">Streets Relief</option>
            <option value="ArcGIS:Imagery:Standard">Imagery</option>
            <option value="ArcGIS:ChartedTerritory">ChartedTerritory</option>
            <option value="ArcGIS:ColoredPencil">ColoredPencil</option>
            <option value="ArcGIS:Nova">Nova</option>
            <option value="ArcGIS:Midcentury">Midcentury</option>
            <option value="OSM:Standard">OSM</option>
            <option value="OSM:Streets">OSM:Streets</option>
          </select>
    
        </div>
    
        <script>
          const apiKey = "YOUR-API-KEY";
          const map = new ol.Map({ target: "map" });
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles";
          const url = (name) => `${baseUrl}/${name}?type=style&apiKey=${apiKey}`;
    
          const setBasemap = (name) => {
    
            // Clear out existing layers.
            map.setLayerGroup(new ol.layer.Group());
    
            // Instantiate the given basemap layer.
            olms(map, url(name));
          };
    
          setBasemap('ArcGIS:Streets');
    
          const basemapsSelectElement = document.querySelector('#basemaps');
    
          basemapsSelectElement.addEventListener('change', (e) => {
            setBasemap(e.target.value);
          });
    
        </script>
    
      </body>
    </html>
  3. In the wrapper element, add a <select> element with basemap layer enumerations and labels.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                    
    
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Change the basemap layer</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <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;
          }
          #basemaps {
            padding: 4px 8px;
            font-size: 16px;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <div id="basemaps-wrapper">
    
          <select id="basemaps">
            <option value="ArcGIS:Streets">Streets</option>
            <option value="ArcGIS:Navigation">Navigation</option>
            <option value="ArcGIS:Topographic">Topographic</option>
            <option value="ArcGIS:LightGray">Light Gray</option>
            <option value="ArcGIS:DarkGray">Dark gray</option>
            <option value="ArcGIS:StreetsRelief">Streets Relief</option>
            <option value="ArcGIS:Imagery:Standard">Imagery</option>
            <option value="ArcGIS:ChartedTerritory">ChartedTerritory</option>
            <option value="ArcGIS:ColoredPencil">ColoredPencil</option>
            <option value="ArcGIS:Nova">Nova</option>
            <option value="ArcGIS:Midcentury">Midcentury</option>
            <option value="OSM:Standard">OSM</option>
            <option value="OSM:Streets">OSM:Streets</option>
          </select>
    
        </div>
    
        <script>
          const apiKey = "YOUR-API-KEY";
          const map = new ol.Map({ target: "map" });
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles";
          const url = (name) => `${baseUrl}/${name}?type=style&apiKey=${apiKey}`;
    
          const setBasemap = (name) => {
    
            // Clear out existing layers.
            map.setLayerGroup(new ol.layer.Group());
    
            // Instantiate the given basemap layer.
            olms(map, url(name));
          };
    
          setBasemap('ArcGIS:Streets');
    
          const basemapsSelectElement = document.querySelector('#basemaps');
    
          basemapsSelectElement.addEventListener('change', (e) => {
            setBasemap(e.target.value);
          });
    
        </script>
    
      </body>
    </html>

Set the initial basemap layer

When the page loads, initialize the basemap layer to the first option in the menu.

  1. Create a function called url that computes the style service URL for a basemap layer.

    Add line.Add line.
                                                                                                    
    
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Change the basemap layer</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <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;
          }
          #basemaps {
            padding: 4px 8px;
            font-size: 16px;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <div id="basemaps-wrapper">
    
          <select id="basemaps">
            <option value="ArcGIS:Streets">Streets</option>
            <option value="ArcGIS:Navigation">Navigation</option>
            <option value="ArcGIS:Topographic">Topographic</option>
            <option value="ArcGIS:LightGray">Light Gray</option>
            <option value="ArcGIS:DarkGray">Dark gray</option>
            <option value="ArcGIS:StreetsRelief">Streets Relief</option>
            <option value="ArcGIS:Imagery:Standard">Imagery</option>
            <option value="ArcGIS:ChartedTerritory">ChartedTerritory</option>
            <option value="ArcGIS:ColoredPencil">ColoredPencil</option>
            <option value="ArcGIS:Nova">Nova</option>
            <option value="ArcGIS:Midcentury">Midcentury</option>
            <option value="OSM:Standard">OSM</option>
            <option value="OSM:Streets">OSM:Streets</option>
          </select>
    
        </div>
    
        <script>
          const apiKey = "YOUR-API-KEY";
          const map = new ol.Map({ target: "map" });
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles";
          const url = (name) => `${baseUrl}/${name}?type=style&apiKey=${apiKey}`;
    
          const setBasemap = (name) => {
    
            // Clear out existing layers.
            map.setLayerGroup(new ol.layer.Group());
    
            // Instantiate the given basemap layer.
            olms(map, url(name));
          };
    
          setBasemap('ArcGIS:Streets');
    
          const basemapsSelectElement = document.querySelector('#basemaps');
    
          basemapsSelectElement.addEventListener('change', (e) => {
            setBasemap(e.target.value);
          });
    
        </script>
    
      </body>
    </html>
  2. Create a function called setBasemap that will clear existing layers and use olms to instantiate a basemap layer by name.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                    
    
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Change the basemap layer</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <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;
          }
          #basemaps {
            padding: 4px 8px;
            font-size: 16px;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <div id="basemaps-wrapper">
    
          <select id="basemaps">
            <option value="ArcGIS:Streets">Streets</option>
            <option value="ArcGIS:Navigation">Navigation</option>
            <option value="ArcGIS:Topographic">Topographic</option>
            <option value="ArcGIS:LightGray">Light Gray</option>
            <option value="ArcGIS:DarkGray">Dark gray</option>
            <option value="ArcGIS:StreetsRelief">Streets Relief</option>
            <option value="ArcGIS:Imagery:Standard">Imagery</option>
            <option value="ArcGIS:ChartedTerritory">ChartedTerritory</option>
            <option value="ArcGIS:ColoredPencil">ColoredPencil</option>
            <option value="ArcGIS:Nova">Nova</option>
            <option value="ArcGIS:Midcentury">Midcentury</option>
            <option value="OSM:Standard">OSM</option>
            <option value="OSM:Streets">OSM:Streets</option>
          </select>
    
        </div>
    
        <script>
          const apiKey = "YOUR-API-KEY";
          const map = new ol.Map({ target: "map" });
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles";
          const url = (name) => `${baseUrl}/${name}?type=style&apiKey=${apiKey}`;
    
          const setBasemap = (name) => {
    
            // Clear out existing layers.
            map.setLayerGroup(new ol.layer.Group());
    
            // Instantiate the given basemap layer.
            olms(map, url(name));
          };
    
          setBasemap('ArcGIS:Streets');
    
          const basemapsSelectElement = document.querySelector('#basemaps');
    
          basemapsSelectElement.addEventListener('change', (e) => {
            setBasemap(e.target.value);
          });
    
        </script>
    
      </body>
    </html>
  3. Call setBasemap to initialize the basemap layer to the ArcGIS:Streets enumeration to match the first basemap in the select element.

    Add line.
                                                                                                    
    
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Change the basemap layer</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <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;
          }
          #basemaps {
            padding: 4px 8px;
            font-size: 16px;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <div id="basemaps-wrapper">
    
          <select id="basemaps">
            <option value="ArcGIS:Streets">Streets</option>
            <option value="ArcGIS:Navigation">Navigation</option>
            <option value="ArcGIS:Topographic">Topographic</option>
            <option value="ArcGIS:LightGray">Light Gray</option>
            <option value="ArcGIS:DarkGray">Dark gray</option>
            <option value="ArcGIS:StreetsRelief">Streets Relief</option>
            <option value="ArcGIS:Imagery:Standard">Imagery</option>
            <option value="ArcGIS:ChartedTerritory">ChartedTerritory</option>
            <option value="ArcGIS:ColoredPencil">ColoredPencil</option>
            <option value="ArcGIS:Nova">Nova</option>
            <option value="ArcGIS:Midcentury">Midcentury</option>
            <option value="OSM:Standard">OSM</option>
            <option value="OSM:Streets">OSM:Streets</option>
          </select>
    
        </div>
    
        <script>
          const apiKey = "YOUR-API-KEY";
          const map = new ol.Map({ target: "map" });
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles";
          const url = (name) => `${baseUrl}/${name}?type=style&apiKey=${apiKey}`;
    
          const setBasemap = (name) => {
    
            // Clear out existing layers.
            map.setLayerGroup(new ol.layer.Group());
    
            // Instantiate the given basemap layer.
            olms(map, url(name));
          };
    
          setBasemap('ArcGIS:Streets');
    
          const basemapsSelectElement = document.querySelector('#basemaps');
    
          basemapsSelectElement.addEventListener('change', (e) => {
            setBasemap(e.target.value);
          });
    
        </script>
    
      </body>
    </html>

Set the selected basemap layer

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

  1. Extract the <select> DOM element using document.querySelector.

    Add line.
                                                                                                    
    
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Change the basemap layer</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <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;
          }
          #basemaps {
            padding: 4px 8px;
            font-size: 16px;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <div id="basemaps-wrapper">
    
          <select id="basemaps">
            <option value="ArcGIS:Streets">Streets</option>
            <option value="ArcGIS:Navigation">Navigation</option>
            <option value="ArcGIS:Topographic">Topographic</option>
            <option value="ArcGIS:LightGray">Light Gray</option>
            <option value="ArcGIS:DarkGray">Dark gray</option>
            <option value="ArcGIS:StreetsRelief">Streets Relief</option>
            <option value="ArcGIS:Imagery:Standard">Imagery</option>
            <option value="ArcGIS:ChartedTerritory">ChartedTerritory</option>
            <option value="ArcGIS:ColoredPencil">ColoredPencil</option>
            <option value="ArcGIS:Nova">Nova</option>
            <option value="ArcGIS:Midcentury">Midcentury</option>
            <option value="OSM:Standard">OSM</option>
            <option value="OSM:Streets">OSM:Streets</option>
          </select>
    
        </div>
    
        <script>
          const apiKey = "YOUR-API-KEY";
          const map = new ol.Map({ target: "map" });
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles";
          const url = (name) => `${baseUrl}/${name}?type=style&apiKey=${apiKey}`;
    
          const setBasemap = (name) => {
    
            // Clear out existing layers.
            map.setLayerGroup(new ol.layer.Group());
    
            // Instantiate the given basemap layer.
            olms(map, url(name));
          };
    
          setBasemap('ArcGIS:Streets');
    
          const basemapsSelectElement = document.querySelector('#basemaps');
    
          basemapsSelectElement.addEventListener('change', (e) => {
            setBasemap(e.target.value);
          });
    
        </script>
    
      </body>
    </html>
  2. Call setBasemap in response to change events on the <select> element.

    Add line.Add line.Add line.
                                                                                                    
    
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Change the basemap layer</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <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;
          }
          #basemaps {
            padding: 4px 8px;
            font-size: 16px;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <div id="basemaps-wrapper">
    
          <select id="basemaps">
            <option value="ArcGIS:Streets">Streets</option>
            <option value="ArcGIS:Navigation">Navigation</option>
            <option value="ArcGIS:Topographic">Topographic</option>
            <option value="ArcGIS:LightGray">Light Gray</option>
            <option value="ArcGIS:DarkGray">Dark gray</option>
            <option value="ArcGIS:StreetsRelief">Streets Relief</option>
            <option value="ArcGIS:Imagery:Standard">Imagery</option>
            <option value="ArcGIS:ChartedTerritory">ChartedTerritory</option>
            <option value="ArcGIS:ColoredPencil">ColoredPencil</option>
            <option value="ArcGIS:Nova">Nova</option>
            <option value="ArcGIS:Midcentury">Midcentury</option>
            <option value="OSM:Standard">OSM</option>
            <option value="OSM:Streets">OSM:Streets</option>
          </select>
    
        </div>
    
        <script>
          const apiKey = "YOUR-API-KEY";
          const map = new ol.Map({ target: "map" });
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles";
          const url = (name) => `${baseUrl}/${name}?type=style&apiKey=${apiKey}`;
    
          const setBasemap = (name) => {
    
            // Clear out existing layers.
            map.setLayerGroup(new ol.layer.Group());
    
            // Instantiate the given basemap layer.
            olms(map, url(name));
          };
    
          setBasemap('ArcGIS:Streets');
    
          const basemapsSelectElement = document.querySelector('#basemaps');
    
          basemapsSelectElement.addEventListener('change', (e) => {
            setBasemap(e.target.value);
          });
    
        </script>
    
      </body>
    </html>

Run the app

In CodePen, run your code to display the map.

Use the layer switcher menu at the top right to select and explore different basemap layer styles.

What's Next?

Learn how to use additional ArcGIS location services in these tutorials: