Use the MapLibre ArcGIS plugin to change the basemap style.
Prerequisites
- You need an ArcGIS Location Platform account.
- You need to complete Step 2. Create a basemap session.
Steps
Create a Basemap Control
To let users change the basemap style directly from the map UI, you can create a custom control that implements MapLibre's I
.
-
Create a new
Basemap
that contains a button. This control will serve as the container for the UI elements you will add in the next step.Control Use dark colors for code blocks class BasemapControl { onAdd(map) { const container = document.createElement("div"); container.innerHTML = ` <div class="maplibregl-ctrl maplibregl-ctrl-group"> <button id="basemap-btn" type="button" aria-label="Change basemap"></button> <div id="basemap-menu" style="display: none;"></div> </div>`; const button = container.querySelector("#basemap-btn"); const menu = container.querySelector("#basemap-menu"); // Open/close the menu when button is clicked button.addEventListener("click", () => { menu.style.display = menu.style.display === "none" ? "flex" : "none"; }); return container; } }
-
Add CSS styling to control the layout and appearance of the basemap control and button.
Use dark colors for code blocks html, body, #map { padding: 0; margin: 0; height: 100%; width: 100%; font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #323232; } #basemap-menu { position: absolute; top: 100%; right: 0; margin-right: 30px; white-space: nowrap; max-height: 200px; overflow-y: auto; background: white; z-index: 999; box-shadow: 0 2px 8px 0 #0000000a, 0 4px 16px 0 #00000014; flex-direction: column; } #basemap-menu div { cursor: pointer; padding: 6px 10px; } #basemap-menu div:hover { background: #eee; } .basemap-control button { display: flex; align-items: center; justify-content: center; padding: 0; background: white; cursor: pointer; } #basemap-btn { width: 30px; height: 30px; background: url("/maplibre-gl-js//images/styles.svg") no-repeat center center; background-size: 16px 16px; }
-
Use the MapLibre ArcGIS plugin to populate the
basemap-menu
with ArcGIS styles from the Basemap Styles service. Each style will appear as a clickable item that updates the basemap when selected.Use dark colors for code blocks class BasemapControl { onAdd(map) { const container = document.createElement("div"); container.innerHTML = ` <div class="maplibregl-ctrl maplibregl-ctrl-group"> <button id="basemap-btn" type="button" aria-label="Change basemap"></button> <div id="basemap-menu" style="display: none;"></div> </div>`; const button = container.querySelector("#basemap-btn"); const menu = container.querySelector("#basemap-menu"); maplibreArcGIS.BasemapStyle.getSelf().then((styleResponse) => { styleResponse.styles .filter((s) => !s.deprecated && s.path.startsWith("arcgis/")) // exclude deprecated or non-ArcGIS styles .forEach((s) => { const item = document.createElement("div"); item.textContent = s.name; item.addEventListener("click", () => { basemapStyle.updateStyle({ style: s.path }); menu.style.display = "none" }); menu.appendChild(item); }); }); // Open/close the menu when button is clicked button.addEventListener("click", () => { menu.style.display = menu.style.display === "none" ? "flex" : "none"; }); return container; } }
Attach the Basemap Control
- Add the
Basemap
to the top right of your map, under the navigation control.Control Use dark colors for code blocks map.addControl(new BasemapControl(), "top-right");
Run the app
Run the app.
The map should display a Basemap button on the top right. When you click the button, a selection list will open and allow you to change the basemap style.