Learn how to change a basemap style in a map with the basemap styles service (v1).
The basemap styles service (v1) provides a number basemap layer styles such as topography, streets, and imagery that you can use in maps.
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 Developer or ArcGIS Online account to access the developer dashboard and create an API key.
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or .
Set the API key
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
-
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
-
In CodePen, update
api
to use your key.Key Use dark colors for code blocks const apiKey = "YOUR_API_KEY"; const basemapId = "ArcGIS:Streets"; const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey; olms.apply(map, basemapURL);
Remove basemap references
-
Remove the
basemap
,I d basemap
, and theURL olms
function.Use dark colors for code blocks <script> const map = new ol.Map({ target: "map" }); map.setView( new ol.View({ center: ol.proj.fromLonLat([-118.805, 34.027]), zoom: 12 }) ); const apiKey = "YOUR_API_KEY"; const basemapId = "arcgis/streets"; const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${apiKey}`; olms.apply(map, basemapURL); </script>
Add a basemap selector
OpenLayers does not have a basemap layer switching widget, so you will use the a plain HTML <select>
element.
-
In the
<head>
tag, add CSS that will position the<select>
menu 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 { padding: 4px 8px; font-size: 16px; } </style>
-
In the
<body>
element, add the wrapper tag with an id ofbasemaps-wrapper
.Use dark colors for code blocks <body> <div id="map"></div> <div id="basemaps-wrapper"> </div>
-
In the wrapper element, add a
<select>
element with basemap layer enumerations and labels.Use dark colors for code blocks <div id="basemaps-wrapper"> <select id="basemaps"> <option value="ArcGIS:Streets">ArcGIS:Streets</option> <option value="ArcGIS:Navigation">ArcGIS:Navigation</option> <option value="ArcGIS:Topographic">ArcGIS:Topographic</option> <option value="ArcGIS:LightGray">ArcGIS:LightGray</option> <option value="ArcGIS:DarkGray">ArcGIS:DarkGray</option> <option value="ArcGIS:StreetsRelief">ArcGIS:StreetsRelief</option> <option value="ArcGIS:Imagery:Standard">ArcGIS:Imagery:Standard</option> <option value="ArcGIS:ChartedTerritory">ArcGIS:ChartedTerritory</option> <option value="ArcGIS:ColoredPencil">ArcGIS:ColoredPencil</option> <option value="ArcGIS:Nova">ArcGIS:Nova</option> <option value="ArcGIS:Midcentury">ArcGIS:Midcentury</option> <option value="OSM:Standard">OSM:Standard</option> <option value="OSM:Streets">OSM:Streets</option> </select> </div>
Set the initial basemap layer
When the page loads, initialize the basemap layer to the first option in the menu.
-
Create a function called
url
that computes the style service URL for a basemap layer.Use dark colors for code blocks <script> const apiKey = "YOUR_API_KEY"; 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://basemaps-api.arcgis.com/arcgis/rest/services/styles"; const url = (name) => `${baseUrl}/${name}?type=style&token=${apiKey}`;
-
Create a function called
set
that will clear existing layers and useBasemap olms
to instantiate a basemap layer by name.Use dark colors for code blocks const baseUrl = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles"; const url = (name) => `${baseUrl}/${name}?type=style&token=${apiKey}`; const setBasemap = (name) => { // Clear out existing layers. map.setLayerGroup(new ol.layer.Group()); // Instantiate the given basemap layer. olms.apply(map, url(name)); };
-
Call
set
to initialize the basemap layer to theBasemap ArcGIS:
enumeration to match the first basemap in theStreets select
element.Use dark colors for code blocks const setBasemap = (name) => { // Clear out existing layers. map.setLayerGroup(new ol.layer.Group()); // Instantiate the given basemap layer. olms.apply(map, url(name)); }; setBasemap("ArcGIS:Streets");
Set the selected basemap layer
Call set
when the user selects an option in the <select>
menu.
-
Extract the
<select>
DOM element usingdocument.query
.Selector Use dark colors for code blocks setBasemap("ArcGIS:Streets"); const basemapsSelectElement = document.querySelector("#basemaps");
-
Call
set
in response toBasemap change
events on the<select>
element.Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#basemaps"); basemapsSelectElement.addEventListener("change", (e) => { setBasemap(e.target.value); });
Run the app
In CodePen, run your code to display the map.
Use the layer switcher menu at the top right to select and explore different basemap layer styles.
What's Next?
Learn how to use additional ArcGIS location services in these tutorials:
Change the basemap style (v2)
Switch a basemap style in a map using the basemap styles service v2.
Change the place label language
Switch the language of place labels on a basemap.
Display a custom basemap style
Add a styled vector basemap layer to a map.
Style vector tiles
Change the fill and outline of vector tiles based on their attributes.