Learn how to change a basemap layer from the basemap styles service using MapLibre GL JS.
The basemap styles service (v1) provides a number of basemap layer styles such as topography, streets, and imagery that you can use in maps.
In this tutorial, you use a <select>
dropdown menu to toggle between a number of different basemap layer styles.
Prerequisites
You need an ArcGIS Developer or ArcGIS Online account to access the developer dashboard and create an API key.
Steps
Create a new pen
- Go to CodePen to create a new pen for your application.
Set the API key
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
-
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
-
In CodePen, update
api
to use your key.Key Use dark colors for code blocks const apiKey = "YOUR_API_KEY"; const basemapEnum = "ArcGIS:Streets"; const map = new maplibregl.Map({ container: "map", // the id of the div element style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`, zoom: 12, // starting zoom center: [-118.805, 34.027] // starting location [longitude, latitude] });
Add a selector
-
Create a
<div>
tag to contain the<select>
dropdown menu. Add a basemap layer enumeration for each<options>
element.</options> Use dark colors for code blocks </style> <script src=https://unpkg.com/maplibre-gl@3.2.1/dist/maplibre-gl.js></script> <link href=https://unpkg.com/maplibre-gl@3.2.1/dist/maplibre-gl.css rel="stylesheet" /> </head> <body> <div id="map"></div> <div id="basemaps-wrapper"> <select id="basemaps"> <option value="ArcGIS:Streets">ArcGIS:Streets</option> <option value="ArcGIS:Navigation">ArcGIS:Navigation</option> <option value="ArcGIS:Topographic">ArcGIS:Topographic</option> <option value="ArcGIS:LightGray">ArcGIS:LightGray</option> <option value="ArcGIS:DarkGray">ArcGIS:DarkGray</option> <option value="ArcGIS:StreetsRelief">ArcGIS:StreetsRelief</option> <option value="ArcGIS:Imagery:Standard">ArcGIS:Imagery:Standard</option> <option value="ArcGIS:ChartedTerritory">ArcGIS:ChartedTerritory</option> <option value="ArcGIS:ColoredPencil">ArcGIS:ColoredPencil</option> <option value="ArcGIS:Nova">ArcGIS:Nova</option> <option value="ArcGIS:Midcentury">ArcGIS:Midcentury</option> <option value="OSM:Standard">OSM:Standard</option> <option value="OSM:Streets">OSM:Streets</option> </select> </div>
-
In the
<style>
element, add CSS styling to thebasemaps-wrapper
andbasemaps
.Use dark colors for code blocks <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> <script src=https://unpkg.com/maplibre-gl@3.2.1/dist/maplibre-gl.js></script> <link href=https://unpkg.com/maplibre-gl@3.2.1/dist/maplibre-gl.css rel="stylesheet" />
Set the basemap
Use a function to reference the basemap styles service and a style enumeration to update the map. This will be used when a selection is made.
-
Create a
base
and aUrl url
element. Theurl
element will append thename
of the basemap selected from the dropdown menu.Use dark colors for code blocks <script> const apiKey = "YOUR_API_KEY"; const basemapEnum = "ArcGIS:Streets"; const map = new maplibregl.Map({ container: "map", // the id of the div element style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`, zoom: 12, // starting zoom center: [-118.805, 34.027], // starting location [longitude, latitude] attributionControl: false }).addControl( new maplibregl.AttributionControl({ compact: true // reduces the copyright attributions view }) ); const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles"; const url = (name) => `${baseUrl}/${name}?type=style&token=${apiKey}`;
-
Set the style of the
map
with the new basemap layer.Use dark colors for code blocks const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles"; const url = (name) => `${baseUrl}/${name}?type=style&token=${apiKey}`; const setBasemap = (name) => { // Instantiate the given basemap layer. map.setStyle(url(name)); };
-
Run the code to ensure 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.
-
Set the default basemap to
ArcGIS:
.Streets Use dark colors for code blocks const setBasemap = (name) => { // Instantiate the given basemap layer. map.setStyle(url(name)); }; setBasemap("ArcGIS:Streets");
-
Create a
basemaps
to return the basemap from the selector.Select Element Use dark colors for code blocks setBasemap("ArcGIS:Streets"); const basemapsSelectElement = document.querySelector("#basemaps");
-
Add an event listener to update the map to the new basemap when the selector is changed.
Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps"); basemapsSelectElement.addEventListener("change", (e) => { setBasemap(e.target.value); });
Run the app
In CodePen, run your code to display the map.
You should be able to use the select element to switch between basemap layers.
What's next?
Learn how to use additional ArcGIS location services in these tutorials:

Change the basemap style (v2)
Switch a basemap style in a map using the basemap styles service v2.

Change the place label language
Switch the language of place labels on a basemap.

Display a custom basemap style
Add a styled vector basemap layer to a map.

Style vector tiles
Change the fill and outline of vector tiles based on their attributes.