Learn how to change the static basemap tiles style.
The static basemap tiles service (beta) 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
An ArcGIS Location Platform account.
Steps
Get the starter app
Select a type of authentication below and follow the steps to create a new application.
Set up authentication
Create developer credentials in your portal for the type of authentication you selected.
Set developer credentials
Use the API key or OAuth developer credentials so your application can access location services.
Add a style selector
-
Create a
<div
tag to contain the> <select
dropdown menu. Add the supported basemap style enumerations from the static basemap tiles service (beta) as> <option
s.> Use dark colors for code blocks <div id="map"></div> <div id="basemaps-wrapper"> <select id="basemaps"> <option value="beta/arcgis/outdoor">arcgis/outdoor</option> <option value="beta/arcgis/streets">arcgis/streets</option> <option value="beta/arcgis/streets-night">arcgis/streets-night</option> <option value="beta/arcgis/navigation">arcgis/navigation</option> <option value="beta/arcgis/navigation-night">arcgis/navigation-night</option> <option value="beta/arcgis/light-gray">arcgis/light-gray</option> <option value="beta/arcgis/dark-gray">arcgis/dark-gray</option> <option value="beta/arcgis/imagery/labels">arcgis/imagery/labels</option> <option value="beta/arcgis/oceans/labels">arcgis/oceans/labels</option> <option value="beta/arcgis/community">arcgis/community</option> <option value="beta/arcgis/nova">arcgis/nova</option> <option value="beta/arcgis/midcentury">arcgis/midcentury</option> <option value="beta/arcgis/newspaper">arcgis/newspaper</option> <option value="beta/arcgis/human-geography">arcgis/human-geography</option> <option value="beta/arcgis/human-geography-dark">arcgis/human-geography-dark</option> </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', zoom: 4, center: [-91.2996, 37.1174], });
Set the basemap
Use a function to reference the static basemap tiles service (beta) and a style enumeration to update the map. This will be used when a selection is made.
-
Change the
basemap
toEnum beta/arcgis/outdoor
. This will be the default basemap.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_URL", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapEnum = "beta/arcgis/outdoor";
-
Create a
base
and aUrl url
variables. Theurl
element will 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_URL", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapEnum = "beta/arcgis/outdoor"; const baseUrl = "https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service"; const url = (style) => `${baseUrl}/${style}/static/tile/{z}/{y}/{x}?token=${accessToken}`;
-
Set the style of the map to include a
source
ofraster-tiles
and a layer with an id ofraster-basemap
. Passbasemap
inside theEnum tiles
attribute as the default basemap style.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_URL", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapEnum = "beta/arcgis/outdoor"; const baseUrl = "https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service"; 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], });
-
Run the code to ensure the map displays the default basemap and 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.
-
Create a
basemaps
to return the basemap from the selector.Select Element Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps");
-
Add an event listener to store the selected basemap.
Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps"); 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"); basemapsSelectElement.addEventListener("change", (e) => { const style = e.target.value; const tilesUrl = url(style); map.getSource('raster-tiles').setTiles([tilesUrl]); });
Update attribution
You will update the attribution text for basemap styles service from the previous tutorial to the attribution for static basemap tiles service. You retrieve the data attribution manually from the service URL and display it in the map's attribution control. You also prepend Powered by Esri in the attribution string.
-
Fetch the
copyright
from the service URL. Then, display it and prepend Powered by Esri string in the map's attribution control.Text Use dark colors for code blocks // Add Esri and data attribution // Learn more in https://esriurl.com/attribution fetch(`${baseUrl}/${basemapEnum}/static?token=${accessToken}`) .then(response => response.json()) .then(data => { map._controls[0]._innerContainer.innerText += " | Powered by Esri | " + data.copyrightText; });
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 ArcGIS location services in these tutorials: