Learn how to localize basemap place labels using the basemap styles service.
The basemap styles service provides a number of basemap layer styles such as topography, streets, and imagery that you can use in maps. 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 will create a Calcite combobox to switch between different languages to update the Basemap style and app language.
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
esri
to 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.
Update the map
- Update the
center
andzoom
attributes to focus the display on Europe.Use dark colors for code blocks <arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"> <arcgis-zoom position="top-left"></arcgis-zoom> </arcgis-map>
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 languages from the basemap style API endpoint.
- In a new
<script
at the bottom of the> <body
, use a> require
statement to add the request module.The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD
require
function uses references to determine which modules will be loaded – for example, you can specify"esri/layers/
for loading the FeatureLayer module. After the modules are loaded, they are passed as parameters (e.g.Feature Layer" Feature
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. To learn more about the API's different modules visit the Overview Guide page.Layer Use dark colors for code blocks <script> require([ "esri/request", ], (esriRequest) => { }); </script>
Set the current language
Set the current language of the basemap and the lang
attribute on the body
to Spanish (es
).
Setting the lang
of the application will update the components to use the given language and formatting.
For instance, when the locale is set to Spanish, the zoom component tooltip will say Acercar
instead of Zoom in
.
-
Set the app's locale to Spanish (
es
) by setting thelang
attribute on the documentbody
.Use dark colors for code blocks <body lang="es"> <arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"> <arcgis-zoom position="top-left"></arcgis-zoom> </arcgis-map> </body>
-
Set the basemap from a
style
to thearcgis/outdoor
basemap style with thelanguage
property set to"es"
. This will display the basemap's place labels in Spanish.Use dark colors for code blocks <script> require([ "esri/request", ], (esriRequest) => { const map = document.querySelector("arcgis-map"); map.basemap = { style: { id: "arcgis/outdoor", language: "es" // Spanish language basemap } }; }); </script>
Create a language dropdown
Get the list of languages available from the basemap styles service and add them to a calcite-combobox
component to allow users to select the language of their basemap.
-
Add an
arcgis-placement
component to the map to place the content in the top-right of the map component.Use dark colors for code blocks <arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"> <arcgis-zoom position="top-left"></arcgis-zoom> <arcgis-placement position="top-right"> </arcgis-placement> </arcgis-map>
-
Inside the
arcgis-placement
component, create a div calledbasemap
with aLanguage calcite-combobox
component. Setselection-mode
to single, so only one language can be selected at a time. Add theclear-disabled
attribute so that a language is always selected.Use dark colors for code blocks <arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"> <arcgis-zoom position="top-left"></arcgis-zoom> <arcgis-placement position="top-right"> <div id="basemapLanguage"> <calcite-label>Basemap language</calcite-label> <calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled></calcite-combobox> </div> </arcgis-placement> </arcgis-map>
-
Within the
<style
tags, apply some CSS properties to the> basemap
div.Language Use dark colors for code blocks #basemapLanguage { width: 200px; padding: 10px; background-color: white; }
-
Fetch the list of languages available from the
/self
endpoint for the basemap styles service using theesri
method. SpecifyRequest json
as the response type. This will return a json response with alanguages
property.Use dark colors for code blocks // update combobox values with languages from rest endpoint const basemapStylesEndpoint = "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self"; esriRequest(basemapStylesEndpoint, { responseType: "json" }).then((response) => { });
-
From the response data, use the JavaScript forEach method to iterate through each of the languages and add it as a
calcite-combobox-item
. Set the value to the language code, and the label to the language name. If the language code matches the language of the current basemap, set the item as selected.Use dark colors for code blocks // update combobox values with languages 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 language as a combobox item json.languages.forEach((language, index) => { const comboboxItem = document.createElement("calcite-combobox-item"); comboboxItem.value = language.code; comboboxItem.textLabel = language.name; // if the current basemap language is the same as the combobox item, select it comboboxItem.selected = map.basemap.style.language === language.code; languageCombobox.appendChild(comboboxItem); }); });
Create a basemap language function
Create a function that accepts a language code and updates the basemap style formatted with that language.
-
Define a new function
update
that accepts a language code.Basemap Language Use dark colors for code blocks const updateLanguage = (languageCode) => { };
-
Use the
language
variable to update the application's language and the language of the basemap style.Code Use dark colors for code blocks const updateLanguage = (languageCode) => { // update language in html body document.body.setAttribute('lang', languageCode); // update the basemap language map.basemap = { style: { id: "arcgis/outdoor", language: languageCode } } };
Update the language 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, call the
update
function to update the basemap language and app locale.Basemap Language Use dark colors for code blocks const updateLanguage = (languageCode) => { // update language in html body document.body.setAttribute('lang', languageCode); // update the basemap language map.basemap = { style: { id: "arcgis/outdoor", language: languageCode } } }; const languageCombobox = document.getElementById("languageCombobox"); // when the combobox value changes, update the language languageCombobox.addEventListener("calciteComboboxChange", () => { updateLanguage(languageCombobox.value); });
Run the App
In CodePen, run your code to display the map.
You should be able to use the calcite combobox to update the basemap language and app language.
What's Next?
Learn how to use additional API features and ArcGIS services in these tutorials: