Tutorial: Change language labels for static basemap tiles

Learn how to change language labels for static basemap tiles.

The static basemap tiles service (beta) 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

This tutorial requires an ArcGIS Location Platform account.

Steps

Create a new pen

  1. Go to CodePen to create a new pen for your application.

Get an access token

You need an access token with the correct privileges to access the resources used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s):

    • Privileges
      • Location services > Basemaps
  2. Copy the API key access token to your clipboard when prompted.

  3. In CodePen, update the accessToken variable to use your access token.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    const accessToken = "YOUR_ACCESS_TOKEN";
    const basemapId = "arcgis/outdoor";
    const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`;
    olms.apply(map, basemapURL);
    
  4. 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.

Remove basemap references

  1. Remove the basemapURL and the olms function.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
        <script>
    
          const map = new ol.Map({ target: "map" });
    
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const accessToken = "YOUR_ACCESS_TOKEN";
    
          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>
    
    Expand

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.

  1. 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    <body>
      <div id="map"></div>
    
      <div id="language-wrapper">
        <select id="language">
          <option value="en">English</option>
          <option value="nl">Dutch</option>
          <option value="da">Danish</option>
          <option value="fi">Finnish</option>
          <option value="fr">French</option>
          <option value="de">German</option>
          <option value="nb">Norwegian</option>
        </select>
      </div>
    
    </body>
    
  2. 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        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

  1. Change the map's center to [8.90, 47.14] and zoom level to 7. This will focus the map on Switzerland.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        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 French.

  1. Change the basemapId to beta/arcgis/outdoor. This will be the default basemap.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        const basemapId = "beta/arcgis/outdoor";
    
    Expand
  2. 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.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        const baseUrl = `https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service`;
        const url = (language) => `${baseUrl}/${basemapId}/static/tile/{z}/{y}/{x}?token=${accessToken}&language=${language}`;
    
    
    Expand
  3. Create a defaultLanguage variable for the basemap, which is fr for French.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
      <script>
        const accessToken = "YOUR_ACCESS_TOKEN";
    
        const basemapId = "beta/arcgis/outdoor";
    
        const baseUrl = `https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service`;
        const url = (language) => `${baseUrl}/${basemapId}/static/tile/{z}/{y}/{x}?token=${accessToken}&language=${language}`;
    
        const defaultLanguage = "fr";
    
        const map = new ol.Map({
          target: "map",
    
          view: new ol.View({
    
            center: ol.proj.fromLonLat([8.90, 47.14]), // Switzerland
            zoom: 7,
    
          }),
        });
    
      </script>
    
  4. Create a Tile object that will provide basemap tiles to the map. Pass defaultLanguage into the tile source's URL.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
      <script>
        const accessToken = "YOUR_ACCESS_TOKEN";
    
        const basemapId = "beta/arcgis/outdoor";
    
        const baseUrl = `https://static-map-tiles-api.arcgis.com/arcgis/rest/services/static-basemap-tiles-service`;
        const url = (language) => `${baseUrl}/${basemapId}/static/tile/{z}/{y}/{x}?token=${accessToken}&language=${language}`;
    
        const defaultLanguage = "fr";
    
        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>
    
  5. Pass the tileLayer into the map's layers attribute.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        const map = new ol.Map({
          target: "map",
    
          layers: [tileLayer],
    
          view: new ol.View({
    
            center: ol.proj.fromLonLat([8.90, 47.14]), // Switzerland
            zoom: 7,
    
          }),
        });
    
    Expand
  6. 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 a listener

Use an event listener to register a language change in the <select> element and to update the map.

  1. Create a languageSelectElement to return the basemap from the selector.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        const languageSelectElement = document.querySelector("#language");
    
        });
    
    Expand
  2. Add an event listener to store the selected basemap.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        const languageSelectElement = document.querySelector("#language");
    
        languageSelectElement.addEventListener("change", (e) => {
    
        });
    
    Expand
  3. Update the tileLayer's source URL with the new language.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        const languageSelectElement = document.querySelector("#language");
    
        languageSelectElement.addEventListener("change", (e) => {
    
          const language = e.target.value;
          tileLayer.getSource().setUrl(url(language));
    
        });
    
    Expand

Update attribution

You will update the attribution text for basemap styles service from the previous tutorial to the attribution for static basemap tiles service (beta). 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.

  1. Fetch the copyrightText from the service URL. Then, display it and prepend Powered by Esri string in the map's attribution control.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
        // Add Esri and data attribution
        // Learn more in https://esriurl.com/attribution
        fetch(`${baseUrl}/${basemapId}/static?token=${accessToken}`)
          .then(response => response.json())
          .then(data => {
            tileLayer.getSource().setAttributions(["Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a> | " + data.copyrightText]);
          });
    
    Expand

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?

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.