Learn how to change language labels for static basemap tiles.
The static basemap tiles service provides a number of different styles that you can use in your OpenLayers applications. Each style accepts a language
parameter, which allows you to localize place labels. There are currently more than 5 different languages available.
In this tutorial, you use a <select
dropdown menu to toggle between a number of different language labels for the 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.
Remove basemap references
-
Remove the
basemap
and theURL olms
function.Use dark colors for code blocks <script> /* 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_URI", // The redirect URL registered in your OAuth credentials // portal: "https://www.arcgis.com/sharing/rest" // Your portal URL // }) // const accessToken = session.token; const map = new ol.Map({ target: "map" }); map.setView( new ol.View({ center: ol.proj.fromLonLat([-118.805, 34.027]), zoom: 12 }) ); const basemapId = "arcgis/outdoor"; const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`; olms.apply(map, basemapURL) .then(() => { // Add Esri attribution // Learn more in https://esriurl.com/attribution const source = map.getLayers().item(0).getSource(); source.setAttributions("Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a> | ") }); </script>
Add a language selector
Add a menu that allows users to change the display language of your map. OpenLayers does not have a built-in selector widget, so you will create an HTML <select
element.
-
Create a
<div
tag to contain the> <select
dropdown menu, which you will populate in a later step.> Use dark colors for code blocks <body> <div id="map"></div> <div id="language-wrapper"> <select id="language"></select> </div> </body>
-
In the
<head
tag, add CSS that will position the> <select
menu element and its wrapper in the upper right corner and provide styling.> 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; } #language-wrapper { position: absolute; top: 20px; right: 20px; background: rgba(255, 255, 255, 0); } #language { font-size: 16px; padding: 4px 8px; }
Update the map's viewpoint
-
Change the map's
center
to[8.90, 47.14]
andzoom
level to7
. This will focus the map on Switzerland.Use dark colors for code blocks const map = new ol.Map({ target: "map", view: new ol.View({ center: ol.proj.fromLonLat([8.90, 47.14]), // Switzerland zoom: 7, }), });
Set the basemap
When the page loads, initialize the app to display the static basemap tiles in English.
-
Change the
basemap
toId arcgis/outdoor
. This will be the default basemap.Use dark colors for code blocks const basemapId = "arcgis/outdoor";
-
Create a function called
url
that takes a language as a parameter. This will create the URL of the static basemap tiles for that specified language.Use dark colors for code blocks const baseUrl = `https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service/v1`; const url = (language) => `${baseUrl}/${basemapId}/static/tile/{z}/{y}/{x}?token=${accessToken}&language=${language}`;
-
Create a
default
variable for the basemap, which isLanguage en
for English.Use dark colors for code blocks <script> /* 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_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapId = "arcgis/outdoor"; const baseUrl = `https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service/v1`; const url = (language) => `${baseUrl}/${basemapId}/static/tile/{z}/{y}/{x}?token=${accessToken}&language=${language}`; const defaultLanguage = "en"; const map = new ol.Map({ target: "map", view: new ol.View({ center: ol.proj.fromLonLat([8.90, 47.14]), // Switzerland zoom: 7, }), }); </script>
-
Create a
Tile
object that will provide basemap tiles to the map. Passdefault
into the tile source's URL.Language Use dark colors for code blocks <script> /* 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_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapId = "arcgis/outdoor"; const baseUrl = `https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service/v1`; const url = (language) => `${baseUrl}/${basemapId}/static/tile/{z}/{y}/{x}?token=${accessToken}&language=${language}`; const defaultLanguage = "en"; const tileLayer = new ol.layer.Tile({ source: new ol.source.XYZ({ url: url(defaultLanguage), tileSize: 512, }), }); const map = new ol.Map({ target: "map", view: new ol.View({ center: ol.proj.fromLonLat([8.90, 47.14]), // Switzerland zoom: 7, }), }); </script>
-
Pass the
tile
into the map'sLayer layers
attribute.Use dark colors for code blocks const map = new ol.Map({ target: "map", layers: [tileLayer], view: new ol.View({ center: ol.proj.fromLonLat([8.90, 47.14]), // Switzerland zoom: 7, }), });
-
Create a
language
to return the basemap from the selector.Select Element Use dark colors for code blocks const languageSelectElement = document.querySelector("#language"); });
-
Import
arcgis
to make a request to the static basemap tiles service. This dynamically fetches the available basemap languages and populates theRest.request() select
element.Use dark colors for code blocks <!-- REST JS --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script>
Use dark colors for code blocks arcgisRest.request(`${baseUrl}/self`, { httpMethod: "GET", authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }).then(response => { response.languages.forEach(style => { const option = document.createElement("option"); option.value = style.code option.textContent = style.name languageSelectElement.appendChild(option) }) tileLayer.getSource().setUrl(url(response.languages[0].code)); })
-
Run the code to ensure the map displays the default static basemap tiles in English and that the
<select
element contains different language enumerations.>
Add a listener
Use an event listener to register a language change in the <select
element and to update the map.
-
Add an event listener to store the selected basemap.
Use dark colors for code blocks const languageSelectElement = document.querySelector("#language"); arcgisRest.request(`${baseUrl}/self`, { httpMethod: "GET", authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }).then(response => { response.languages.forEach(style => { const option = document.createElement("option"); option.value = style.code option.textContent = style.name languageSelectElement.appendChild(option) }) tileLayer.getSource().setUrl(url(response.languages[0].code)); }) languageSelectElement.addEventListener("change", (e) => { });
-
Update the
tile
's source URL with the new language.Layer Use dark colors for code blocks const languageSelectElement = document.querySelector("#language"); arcgisRest.request(`${baseUrl}/self`, { httpMethod: "GET", authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }).then(response => { response.languages.forEach(style => { const option = document.createElement("option"); option.value = style.code option.textContent = style.name languageSelectElement.appendChild(option) }) tileLayer.getSource().setUrl(url(response.languages[0].code)); }) languageSelectElement.addEventListener("change", (e) => { const language = e.target.value; tileLayer.getSource().setUrl(url(language)); });
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 arcgisRest.request(`${baseUrl}/${basemapId}/static`, { httpMethod: "GET", authentication: arcgisRest.ApiKeyManager.fromKey(accessToken) }).then(data => { tileLayer.getSource().setAttributions(["Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a> | " + data.copyrightText]); });
Run the app
Run the app.
The map should be displayed in English and you should be able to use the controls to switch between language labels.