Learn how to change the static basemap tiles style.
The ArcGIS Static Basemap Tiles service provides access to raster basemap tiles for the world. The service supports a number of ArcGIS styles such as navigation
, streets
, outdoor
, and light-gray
. The tiles are returned as PNG files.
In this tutorial, you use the dropdown menu to toggle between the different static basemap tiles.
Prerequisites
You need an ArcGIS Location Platform account.
ArcGIS Online and ArcGIS Enterprise accounts are not supported.
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 that access ArcGIS Location Services and secure items.
- 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 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 so your application can access ArcGIS services.
Add a style selector
-
Create a
<div
tag to contain the> <select
dropdown menu, which you will populate in a later step.> Use dark colors for code blocks <div id="map"></div> <div id="basemaps-wrapper"> <select id="basemaps"></select> </div>
-
In the
<style
element, add CSS styling to the> basemaps-wrapper
andbasemaps
.Use dark colors for code blocks <style> body { margin: 0; padding: 0; } html, body, #map { height: 100%; } #basemaps-wrapper { position: absolute; top: 20px; right: 20px; background: rgba(255, 255, 255, 0); } #basemaps { font-size: 16px; padding: 4px 8px; } </style>
Update the map's viewpoint
-
Change the map's
center
to[-91.2996, 37.1174]
andzoom
level to4
. This will focus the map on the United States of America.Use dark colors for code blocks const map = new maplibregl.Map({ container: "map", style: { version: 8, sources: { "raster-tiles": { type: "raster", tiles: [ url(basemapEnum) ], tileSize: 512 } }, layers: [ { id: "raster-basemap", type: "raster", source: "raster-tiles", minzoom: 0, maxzoom: 22 } ] }, zoom: 4, center: [-91.2996, 37.1174] });
Set the basemap
Use a function to reference the ArcGIS Static Basemap Tiles service and a style enumeration to update the map. This will be used when a selection is made.
-
Create a
url
function to append the name of the basemap selected from the dropdown menu.Use dark colors for code blocks /* 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: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapEnum = "arcgis/navigation"; const baseUrl = "https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service/v1"; const url = (style) => `${baseUrl}/${style}/static/tile/{z}/{y}/{x}?token=${accessToken}`; const map = new maplibregl.Map({ container: "map", style: { version: 8, sources: { "raster-tiles": { type: "raster", tiles: [ url(basemapEnum) ], tileSize: 512 } }, layers: [ { id: "raster-basemap", type: "raster", source: "raster-tiles", minzoom: 0, maxzoom: 22 } ] }, zoom: 4, center: [-91.2996, 37.1174] }); // Add Esri and data attribution // Learn more in https://esriurl.com/attribution arcgisRest.request(`${baseUrl}/${basemapEnum}/static`, { httpMethod: "GET", authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }).then((data) => { map._controls[0]._innerContainer.innerText += " | Powered by Esri | " + data.copyrightText; });
-
Modify the
tiles
property of the map to use theurl
function withbasemap
as the default basemap style.Enum Use dark colors for code blocks const map = new maplibregl.Map({ container: "map", style: { version: 8, sources: { "raster-tiles": { type: "raster", tiles: [ url(basemapEnum) ], tileSize: 512 } }, layers: [ { id: "raster-basemap", type: "raster", source: "raster-tiles", minzoom: 0, maxzoom: 22 } ] }, zoom: 4, center: [-91.2996, 37.1174] });
-
Retrieve the
select
element by storing it in abasemaps
variable.Select Element Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps");
-
Import
arcgis
to make a request to the static basemap tiles service. This dynamically fetches the available basemap styles and populates theRest.request() select
element.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", authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }).then((response) => { response.styles.forEach((style) => { const option = document.createElement("option"); option.value = style.path; option.textContent = style.name; basemapsSelectElement.appendChild(option); }); });
-
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.
-
Add an event listener to store the selected basemap.
Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps"); arcgisRest.request(`${baseUrl}/self`, { httpMethod: "GET", authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }).then((response) => { response.styles.forEach((style) => { const option = document.createElement("option"); option.value = style.path; option.textContent = style.name; basemapsSelectElement.appendChild(option); }); }); basemapsSelectElement.addEventListener("change", (e) => { const style = e.target.value; const tilesUrl = url(style); });
-
Update the map's
raster-tiles
with the new basemap style.Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps"); arcgisRest.request(`${baseUrl}/self`, { httpMethod: "GET", authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }).then((response) => { response.styles.forEach((style) => { const option = document.createElement("option"); option.value = style.path; option.textContent = style.name; basemapsSelectElement.appendChild(option); }); }); basemapsSelectElement.addEventListener("change", (e) => { const style = e.target.value; const tilesUrl = url(style); map.getSource("raster-tiles").setTiles([tilesUrl]); });
Run the app
Run the app.
You should be able to use the<select >
element to switch between basemap styles.
What's next?
Learn how to use additional location services in these tutorials: