Learn how to change the basemap style in a map using the static basemap tiles service (beta).
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
This tutorial requires an ArcGIS Location Platform account.
Steps
Create a new pen
- To get started, you can complete the Display a map tutorial or use the .
Get an access token
You need an access token with the correct privileges to access the resources used in this tutorial.
-
Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
-
Copy the API key access token to your clipboard when prompted.
-
In CodePen, update the
access
variable to use your access token.Token Use dark colors for code blocks const accessToken = "YOUR_ACCESS_TOKEN"; const basemapEnum = "arcgis/outdoor"; const map = new maplibregl.Map({ container: "map", // the id of the div element style: `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapEnum}?token=${accessToken}`, zoom: 12, // starting zoom center: [-118.805, 34.027] // starting location [longitude, latitude] });
-
Run the code to ensure the basemap is displayed in the map.
To learn about the other types of authentication available, go to Types of authentication.
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 const accessToken = "YOUR_ACCESS_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 const accessToken = "YOUR_ACCESS_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 const accessToken = "YOUR_ACCESS_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
In CodePen, run your code to display the map.
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: