Learn how to change language labels using the static basemap tiles service (beta).
The static basemap tiles service provides a number of different styles that you can use in your Esri Leaflet 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
This tutorial requires an ArcGIS Location Platform account.
Steps
Create a new pen
- To get started, you can complete the Display a map tutorial or use the .
Get an access token
You need an access token with the correct privileges to access the resources 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
-
Copy the API key access token to your clipboard when prompted.
-
In CodePen, update the
access
variable to use your access token.Token Use dark colors for code blocks const map = L.map("map", { minZoom: 2 }) map.setView([34.02, -118.805], 13); const accessToken = "YOUR_ACCESS_TOKEN"; const basemapEnum = "arcgis/streets"; L.esri.Vector.vectorBasemapLayer(basemapEnum, { token: accessToken }).addTo(map);
-
Run the code to ensure the basemap is displayed in the map.
To learn about the other types of authentication available, go to Types of authentication.
Update script references
The map will use static basemap tiles as its basemap layer. Therefore, you remove references to the esri-leaflet-vector
plugin and the vector basemap layer. Then, you reference the static
plugin.
-
Remove the script references to
esri-leaflet-vector
plugin.Use dark colors for code blocks <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> <title>Esri Leaflet Tutorials: Display a map</title> <!-- Load Leaflet from CDN --> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" crossorigin="" /> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" crossorigin=""></script> <!-- Load Esri Leaflet from CDN --> <script src="https://unpkg.com/esri-leaflet@3.0.10/dist/esri-leaflet.js"></script> <script src="https://unpkg.com/esri-leaflet-vector@4.2.5/dist/esri-leaflet-vector.js"></script>
-
Remove the
basemap
andEnum vector
references.Basemap Layer Use dark colors for code blocks const map = L.map("map", { minZoom: 2 }) map.setView([34.02, -118.805], 13); const accessToken = "YOUR_ACCESS_TOKEN"; const basemapEnum = "arcgis/streets"; L.esri.Vector.vectorBasemapLayer(basemapEnum, { token: accessToken }).addTo(map);
-
Add
<script
tag to reference the library.> Use dark colors for code blocks <!-- Load Leaflet from CDN --> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" crossorigin="" /> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" crossorigin=""></script> <!-- Load Esri Leaflet from CDN --> <script src="https://unpkg.com/esri-leaflet@3.0.10/dist/esri-leaflet.js"></script> <script src="https://unpkg.com/esri-leaflet-static-basemap-tile@1.0.0-beta.1/dist/esri-leaflet-static-basemap-tile.js"></script>
Update the map's viewpoint
-
Change the map's center to
[47.14, 8.90]
and zoom level to7
. This will focus the map on Switzerland.Use dark colors for code blocks const accessToken = "YOUR_ACCESS_TOKEN"; const map = L.map("map", { minZoom: 2 }) map.setView([47.14, 8.90], 7);
Add the language labels
You will get all the available languages for your selected basemap style and create a menu that allows users to change the display language of your map.
-
Create a
basemap
variable that stores the default basemap style,Style beta/arcgis/navigation
.Use dark colors for code blocks const accessToken = "YOUR_ACCESS_TOKEN"; const map = L.map("map", { minZoom: 2 }) map.setView([47.14, 8.90], 7); const basemapStyle = 'beta/arcgis/navigation';
-
Use the
get
method fromSelf L.esri.
to make a request to the static basemap tiles service that returns all available basemap styles and their metadata.Static. Util Use dark colors for code blocks const accessToken = "YOUR_ACCESS_TOKEN"; const map = L.map("map", { minZoom: 2 }) map.setView([47.14, 8.90], 7); const basemapStyle = 'beta/arcgis/navigation'; L.esri.Static.Util.getSelf(accessToken).then((data) => { })
-
Create a
languages
object to store all the returned languages available forbeta/arcgis/navigation
.Use dark colors for code blocks L.esri.Static.Util.getSelf(accessToken).then((data) => { const languages = {}; })
-
Iterate through each language and create an
L.esri.
, passing the language code to theStatic.static Basemap Tile Layer language
property. Store it inside thelanguages
object.Use dark colors for code blocks L.esri.Static.Util.getSelf(accessToken).then((data) => { const languages = {}; data.languages.forEach(language => { languages[language.name] = L.esri.Static.staticBasemapTileLayer(basemapStyle, { token: accessToken, language: language.code }) }) })
-
Create a
Layers
control that referenceslanguages
and add it to your map.Use dark colors for code blocks L.esri.Static.Util.getSelf(accessToken).then((data) => { const languages = {}; data.languages.forEach(language => { languages[language.name] = L.esri.Static.staticBasemapTileLayer(basemapStyle, { token: accessToken, language: language.code }) }) L.control.layers(languages, null, { collapsed: false }).addTo(map); })
-
Append
add
to theTo French
entry so that it is the default language when the application loads.Use dark colors for code blocks L.esri.Static.Util.getSelf(accessToken).then((data) => { const languages = {}; data.languages.forEach(language => { languages[language.name] = L.esri.Static.staticBasemapTileLayer(basemapStyle, { token: accessToken, language: language.code }) }) L.control.layers(languages, null, { collapsed: false }).addTo(map); languages['French'].addTo(map); })
Run the app
In CodePen, run your code to display the map.
The map should be displayed in French and you should be able to use the controls to switch between language labels.
What's next?
Learn how to use additional ArcGIS location services in these tutorials: