Learn how to change language labels for basemap styles.
The basemap styles service provides a number of different styles that you can use in your MapLibre GL JS applications. Each style accepts a language
parameter, which allows you to localize place labels. There are currently over 30 different languages available.
In this tutorial, you use a <select
dropdown menu to toggle between a number of different place label languages.
Prerequisites
An ArcGIS Location Platform or ArcGIS Online 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
,Id 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_URL", // 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.
-
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; } #languages-wrapper { position: absolute; top: 20px; right: 20px; } #languages { font-size: 16px; padding: 4px 8px; } </style>
-
In the
<body
element, add the wrapper tag with an id of> languages-wrapper
.Use dark colors for code blocks <body> <div id="map"></div> <div id="languages-wrapper"> </div>
-
In the wrapper element, add a
<select
element that contains an enumeration for each language available through the basemap styles service.> Use dark colors for code blocks <div id="languages-wrapper"> <select id="languages"> <option value="global">Global</option> <option value="ar">Arabic</option> <option value="bs">Bosnian</option> <option value="bg">Bulgarian</option> <option value="ca">Catalan</option> <option value="zh-CN">Chinese (Simplified)</option> <option value="zh-HK">Chinese (Hong Kong)</option> <option value="zh-TW">Chinese (Taiwan)</option> <option value="hr">Croatian</option> <option value="cs">Czech</option> <option value="da">Danish</option> <option value="nl">Dutch</option> <option value="en">English</option> <option value="et">Estonian</option> <option value="fi">Finnish</option> <option value="fr">French</option> <option value="de">German</option> <option value="el">Greek</option> <option value="he">Hebrew</option> <option value="hu">Hungarian</option> <option value="id">Indonesian</option> <option value="it">Italian</option> <option value="ja">Japanese</option> <option value="ko">Korean</option> <option value="lv">Latvian</option> <option value="lt">Lithuanian</option> <option value="nb">Norwegian</option> <option value="pl">Polish</option> <option value="pt-BR">Portugese (Brazil)</option> <option value="pt-PT">Portugese (Portugal)</option> <option value="ro">Romanian</option> <option value="ru">Russian</option> <option value="sr">Serbian</option> <option value="sk">Slovak</option> <option value="sl">Slovenian</option> <option value="es">Spanish</option> <option value="sv">Swedish</option> <option value="th">Thai</option> <option value="tr">Turkish</option> <option value="uk" selected>Ukrainian</option> <option value="vi">Vietnamese</option> </select> </div>
Set the initial language
When the page loads, initialize the app to initially display in Ukrainian.
-
Create a function called
url
that creates the URL for a basemap style.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_URL", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const map = new ol.Map({ target: "map" }); map.setView( new ol.View({ center: ol.proj.fromLonLat([48.8790, 35.7088]), zoom: 3 }) ); const baseUrl = "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles"; const url = (language) => `${baseUrl}/arcgis/outdoor?token=${accessToken}&language=${language}`;
-
Create a function called
set
that will clear existing basemap layers and useLanguage olms
to instantiate a basemap layer in the new language.Use dark colors for code blocks const baseUrl = "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles"; const url = (language) => `${baseUrl}/arcgis/outdoor?token=${accessToken}&language=${language}`; const setLanguage = (language) => { // Clear out existing layers. map.setLayerGroup(new ol.layer.Group()); // Instantiate the given basemap layer. olms.apply(map, url(language)) .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> | ") }); };
-
Call
set
to initialize the basemap layer to the Ukrainian (Language uk
) enumeration.Use dark colors for code blocks const setLanguage = (language) => { // Clear out existing layers. map.setLayerGroup(new ol.layer.Group()); // Instantiate the given basemap layer. olms.apply(map, url(language)) .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> | ") }); }; setLanguage("uk");
Add an event listener
Call set
when the user selects an option in the <select
menu.
-
Access the
<select
element using its id.> Use dark colors for code blocks setLanguage("uk"); const basemapsSelectElement = document.querySelector("#languages");
-
Add an event listener that calls
set
when theLanguage <select
element is changed.> Use dark colors for code blocks const basemapsSelectElement = document.querySelector("#languages"); basemapsSelectElement.addEventListener("change", (e) => { setLanguage(e.target.value); });
Run the app
Run the app.
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
Switch a basemap style in a map using the basemap styles service.
Display a custom basemap style
Add a styled vector basemap layer to a map.
Change the static basemap tiles style
Change the basemap style in a map using the static basemap tiles service (beta).
Add a feature layer
Add features from feature layers to a map.