Learn how to change a basemap style
The ArcGIS Basemap Styles service
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 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
Remove basemap references
-
Remove the
basemap,Id basemap, and theURL olmsfunction.Use dark colors for code blocks <script> /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "https://www.arcgis.com/sharing/rest" // Your portal URL // }) // const accessToken = session.token; const map = new ol.Map({ target: "map" }); map.setView( new ol.View({ center: ol.proj.fromLonLat([-20, 30]), zoom: 3 }) ); const basemapId = "arcgis/outdoor"; const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`; olms.apply(map, basemapURL) .then(() => { // Add Esri attribution // Learn more in https://esriurl.com/attribution const source = map.getLayers().item(0).getSource(); const poweredByEsriString = "Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a> | "; const attributionFn = source.getAttributions(); if (attributionFn) { source.setAttributions((ViewStateLayerStateExtent) => { return [poweredByEsriString, ...attributionFn(ViewStateLayerStateExtent)]; }); } else source.setAttributions(poweredByEsriString); }); </script>
Add a basemap selector
OpenLayers does not have a basemap layer<select element.
-
In the
<headtag, add CSS that will position the> <selectmenu wrapper element in the upper right corner and provide styling.> 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; } #basemaps { font-size: 16px; padding: 4px 8px; } </style> -
In the
<bodyelement, add the wrapper tag with an id of> basemaps-wrapper.Use dark colors for code blocks <body> <div id="map"></div> <div id="basemaps-wrapper"> </div> -
In the wrapper element, add a
<selectelement, which you will populate in a later step.> Use dark colors for code blocks <div id="basemaps-wrapper"> <select id="basemaps"></select> </div>
Set the initial basemap layer
When the page loads, initialize the basemap layer
-
Create a function called
urlthat computes the style service URL for a basemap layer.Use dark colors for code blocks <script> const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "https://www.arcgis.com/sharing/rest" // Your portal URL // }) // const accessToken = session.token; 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://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles"; const url = (style) => `${baseUrl}/${style}?token=${accessToken}`; -
Create a function called
setthat will clear existing layers and useBasemap olmsto instantiate a basemap layer by name.Use dark colors for code blocks const baseUrl = "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles"; const url = (style) => `${baseUrl}/${style}?token=${accessToken}`; const setBasemap = (style) => { // Clear out existing layers. map.setLayerGroup(new ol.layer.Group()); // Instantiate the given basemap layer. olms.apply(map, url(style)).then(() => { // Add Esri attribution // Learn more in https://esriurl.com/attribution const source = map.getLayers().item(0).getSource(); const poweredByEsriString = "Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a> | "; const attributionFn = source.getAttributions(); if (attributionFn) { source.setAttributions((ViewStateLayerStateExtent) => { return [poweredByEsriString, ...attributionFn(ViewStateLayerStateExtent)]; }); } else source.setAttributions(poweredByEsriString); }); }; -
Extract the
<selectDOM element using> document.queryto populate it.Selector Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps"); -
Import
arcgisto make a request to the Basemap Styles service. This dynamically fetches the available basemap styles and populates theRest.request() selectelement.Use dark colors for code blocks <!-- REST JS --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script>Use dark colors for code blocks arcgisRest.request(`${baseUrl}/self`, { httpMethod: "GET" }).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); }); }); -
Call
setto initialize the basemap layer to the first basemap in theBasemap selectelement.Use dark colors for code blocks arcgisRest.request(`${baseUrl}/self`, { httpMethod: "GET" }).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); });
Set the selected basemap layer
Call set when the user selects an option in the <select menu.
-
Call
setin response toBasemap changeevents on the<selectelement.> Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps"); arcgisRest.request(`${baseUrl}/self`, { httpMethod: "GET" }).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.
Use the layer switcher menu at the top right to select and explore different basemap styles.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.

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

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