Learn how to change the basemap style in a map using the Basemap styles service.
The basemap styles service provides a number of ArcGIS basemap layer styles such as topography, navigation, outdoors, and imagery that you can use in maps.
In this tutorial, you will use a Calcite Combobox to select and display different basemap styles.
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or .
Get an access token
You need an access token with the correct privileges to access the location services 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
- In CodePen, set
esrito your access token.Config.api Key Use dark colors for code blocks var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN", };
To learn about other ways to get an access token, go to Types of authentication.
Add modules
The request module can be used to request data from a remote server or file. We'll use this module to fetch the available styles from the basemap style API endpoint.
- In a new
<scriptat the bottom of the> <body, use> $arcgis.import()to add the request module.The ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The
$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK's different modules, visit the References page.Use dark colors for code blocks <script type="module"> const esriRequest = await $arcgis.import("@arcgis/core/request.js"); </script>
Create a style dropdown
Get the list of styles available from the basemap style service and add them to a calcite-combobox component to allow users to change the style of their basemap.
-
Create a div called
basemapwith aStyles calcite-comboboxcomponent, placed in thetop-rightslot of the map. Setselection-modeto single, so only one style can be selected at a time. Add theclear-disabledattribute so that a style is always selected.Use dark colors for code blocks <arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"> <arcgis-zoom slot="top-left"></arcgis-zoom> <div id="basemapStyles" slot="top-right"> <calcite-label> Basemap Style: <calcite-combobox id="styleCombobox" selection-mode="single" clear-disabled></calcite-combobox> </calcite-label> </div> </arcgis-map> -
Within the
<styletags, apply some CSS properties to the> basemapdiv.Styles Use dark colors for code blocks #basemapStyles { background-color: white; border-radius: 5px; padding: 10px; width: 250px; } -
Create a variable for the Map component and the combobox and wait for the view to be ready.
Use dark colors for code blocks const viewElement = document.querySelector("arcgis-map"); const styleCombobox = document.querySelector("#styleCombobox"); // wait for the view to be ready await viewElement.viewOnReady(); -
Fetch the list of styles available from the
/selfendpoint for the basemap styles service using theesrimethod. SpecifyRequest jsonas the response type. This will return a json response with astylesproperty.Use dark colors for code blocks // update combobox values with styles from rest endpoint const basemapStylesEndpoint = "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self"; esriRequest(basemapStylesEndpoint, { responseType: "json", }).then((response) => { const json = response.data; // add each style as a combobox item json.styles.forEach((style) => { }); }); -
From the response data, use the JavaScript forEach method to iterate through each of the styles and add each style as a
calcite-combobox-item. Set the value to the style path, and the label to the style name. If the style path matches the current basemap, set the item as selected.Use dark colors for code blocks // update combobox values with styles from rest endpoint const basemapStylesEndpoint = "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self"; esriRequest(basemapStylesEndpoint, { responseType: "json", }).then((response) => { const json = response.data; // add each style as a combobox item json.styles.forEach((style) => { // if the style is complete and not deprecated, add it to the combobox if (style.complete && !style.deprecated) { const comboboxItem = document.createElement("calcite-combobox-item"); comboboxItem.value = style.path; comboboxItem.heading = style.name; // if the current basemap style is the same as the combobox item, select it comboboxItem.selected = viewElement.basemap.style.id === style.path; styleCombobox.appendChild(comboboxItem); } }); });
Update the style on user input
Use the calcite event to recognize when the user has selected a new language.
- Add an event listener to watch for changes on the combobox. When the combobox value has changed, update the basemap style to the selected item.
Use dark colors for code blocks styleCombobox.addEventListener("calciteComboboxChange", (event) => { const basemapId = event.target.value; if (basemapId) { // update the basemap style viewElement.basemap = basemapId; } });
Run the App
In CodePen, run your code to display the map.
Use the Calcite combobox in the top right to select and explore different basemap styles from the Basemap styles service (v2).
What's Next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: