Learn how to change a basemap style
The ArcGIS Basemap Styles service
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 and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications
A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. that access ArcGIS Location ServicesArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. and secure itemsAn item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. and access resources their behalf. - Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Use the API key or OAuth developer credentials
Add a style selector
-
In the wrapper element, add a
<selectelement, which you will populate in a later step.> Use dark colors for code blocks </style> <!-- Load MapLibre GL JS from CDN --> <script src="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.js"></script> <link href="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- Load MapLibre ArcGIS from CDN --> <script src="https://unpkg.com/@esri/maplibre-arcgis@1.1.0/dist/umd/maplibre-arcgis.min.js"></script> </head> <body> <div id="map"></div> <div id="basemaps-wrapper"> <select id="basemaps"></select> </div> -
In the
<styleelement, add CSS styling to the> basemaps-wrapperandbasemaps.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> <!-- Load MapLibre GL JS from CDN --> <script src="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.js"></script> <link href="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- Load MapLibre ArcGIS from CDN --> <script src="https://unpkg.com/@esri/maplibre-arcgis@1.1.0/dist/umd/maplibre-arcgis.min.js"></script>
Set the basemap
Using the MapLibre ArcGIS plugin, reference the Basemap Styles service
-
Comment out the map style as it will be updated dynamically based on the user's selection.
Use dark colors for code blocks Copy // const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { // style: 'arcgis/outdoor', // token: accessToken // }); -
Define a function to update the map style using the selected basemap layer.
Use dark colors for code blocks const setBasemap = (selection) => { const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: selection, token: accessToken }); }; -
Select the DOM element used for the basemap selector.
Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps"); -
Use the MapLibre ArcGIS plugin to make a request to the Basemap Styles service. This dynamically fetches the available basemap styles and populates the
selectelement.Use dark colors for code blocks 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); }); }); -
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.
Use dark colors for code blocks 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); }); -
Run the code to ensure that the
<selectelement contains different basemap enumerations.>
Add an event listener
Call set when the user selects an option in the <select menu.
-
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"); 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); });
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:

Change the basemap style
Switch a vector basemap style on a map.

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

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

Display basemap places
Display places of interest on a basemap and get place details.

Change the basemap style
Switch a static basemap style on a map.