Learn how to change language labels for static basemap tiles service (beta).
The static basemap tiles service (beta) provides access to raster basemap tiles for the world. The service supports a number of ArcGIS styles such as navigation
, streets
, outdoor
, and light-gray
. The tiles are returned as PNG files.
In this tutorial, you use the dropdown menu to toggle between different languages 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 created in the previous step in your application.
Get a Cesium ion access token
All Cesium applications must use an access token provided through Cesium ion. This token allows you to access assets such as Cesium World Terrain in your application.
-
Go to your Cesium ion dashboard to generate an access token. Copy the key to your clipboard.
-
Create a
cesium
variable and replaceAccess Token YOUR
with the access token you copied from the Cesium ion dashboard._CESIUM _ACCESS _TOKEN 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; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; </script>
-
Configure
Cesium.
with the Cesium access token to validate the application.Ion.default Access Token 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; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; Cesium.Ion.defaultAccessToken = cesiumAccessToken; </script>
Add a language selector
Add a menu that allows users to change the display language of your map. CesiumJS does not have a built-in selector widget, so you will create an HTML <select
element.
-
In the
<body
element, add a> select
element that contains an enumeration for each language available through the static basemap tiles service (beta).Use dark colors for code blocks <body> <div id="cesiumContainer"></div> <div id="language-wrapper"> <select id="language"> <option value="fr">French</option> <option value="en">English</option> <option value="nl">Dutch</option> <option value="da">Danish</option> <option value="fi">Finnish</option> <option value="de">German</option> <option value="nb">Norwegian</option> </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 <style> html, body, #cesiumContainer { margin: 0; padding: 0; width: 100%; height: 100%; } #language-wrapper { position: absolute; top: 20px; left: 20px; background: rgba(255, 255, 255, 0); } #language { font-size: 16px; padding: 4px 8px; } </style>
Update the camera's viewpoint
-
Change the camera's
destination
to8.20, 46.90, 300000
. This will focus the camera on Switzerland.Use dark colors for code blocks viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(8.20, 46.90, 300000), orientation: { heading: Cesium.Math.toRadians(0.0), pitch: Cesium.Math.toRadians(-90.0), } });
Remove base layer references
- Remove
arc
,Gis Imagery terrain
, and thebase
properties from the viewer.Layer Use dark colors for code blocks const arcGisImagery = Cesium.ArcGisMapServerImageryProvider.fromBasemapType(Cesium.ArcGisBaseMapType.SATELLITE); const viewer = new Cesium.Viewer("cesiumContainer", { baseLayer: Cesium.ImageryLayer.fromProviderAsync(arcGisImagery), terrain: Cesium.Terrain.fromWorldTerrain(), timeline: false, animation: false, geocoder: false });
Set the basemap
When the scene loads, initialize the app to display the static basemap tiles in French.
-
Create a constant variable called
basemap
to define the default basemap style. Since the service is still in beta, we will appendEnum beta/
beforearcgis/outdoor
.Use dark colors for code blocks const basemapEnum = 'beta/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 basemapEnum = 'beta/arcgis/outdoor'; const baseUrl = `https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service/${basemapEnum}/static` const url = (language) => `${baseUrl}/tile/{z}/{y}/{x}?token=${accessToken}&language=${language}`;
-
Create a
default
variable for the basemap, which isLanguage fr
for French.Use dark colors for code blocks const basemapEnum = 'beta/arcgis/outdoor'; const baseUrl = `https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service/${basemapEnum}/static` const url = (language) => `${baseUrl}/tile/{z}/{y}/{x}?token=${accessToken}&language=${language}`; const defaultLanguage = "fr";
-
Fetch the service URL to retrieve the basemap JSON. Then, create an
Url
object from the JSON.Template Imagery Provider Use dark colors for code blocks fetch(`${baseUrl}?token=${accessToken}`) .then(response => response.json()) .then(data => { const imageryProvider = new Cesium.UrlTemplateImageryProvider({ url: url(defaultLanguage), tileHeight: 512, }); });
-
Create a
basemap
and instantiate aCredit Credit
object from thecopyright
string of the JSON. CesiumJS does not provide attribution automatically for static basemap tiles service (beta). Therefore, you are required to add this attribution manually.Text Use dark colors for code blocks let basemapCredit; fetch(`${baseUrl}?token=${accessToken}`) .then(response => response.json()) .then(data => { basemapCredit = data.copyrightText const imageryProvider = new Cesium.UrlTemplateImageryProvider({ url: url(defaultLanguage), tileHeight: 512, credit: basemapCredit }); });
-
Add the
imagery
to the viewer.Provider Use dark colors for code blocks fetch(`${baseUrl}?token=${accessToken}`) .then(response => response.json()) .then(data => { basemapCredit = data.copyrightText const imageryProvider = new Cesium.UrlTemplateImageryProvider({ url: url(defaultLanguage), tileHeight: 512, credit: basemapCredit }); viewer.imageryLayers.addImageryProvider(imageryProvider); });
-
Run the code to ensure the map displays the default static basemap tiles in French and that the
<select
element contains different language enumerations.>
Add an event listener
Use an event listener to register a language change in the <select
element and to update the scene.
-
Add an event listener for the
#language
select element.Use dark colors for code blocks document.querySelector("#language").addEventListener("change", (e) => { });
-
Create a new
Url
using the selected language.Template Imagery Provider Use dark colors for code blocks document.querySelector("#language").addEventListener("change", (e) => { const newImageryProvider = new Cesium.UrlTemplateImageryProvider({ url: url(e.target.value), tileWidth: 512, tileHeight: 512, credit: basemapCredit }); });
-
Update the viewer with the new language.
Use dark colors for code blocks document.querySelector("#language").addEventListener("change", (e) => { const newImageryProvider = new Cesium.UrlTemplateImageryProvider({ url: url(e.target.value), tileWidth: 512, tileHeight: 512, credit: basemapCredit }); viewer.imageryLayers.removeAll(); viewer.imageryLayers.addImageryProvider(newImageryProvider); });
Run the app
Run the app.
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: