---
title: "How to change the map labels language based on visitor's location | JavaScript maps SDK"
description: "This tutorial shows how to automatically change the map labels language based on visitor's location usign the MapTiler Geolocation API."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# How to change the map labels language based on visitor’s location

This tutorial demonstrates the process of automatically adjusting the language of map labels based on the visitor’s location using the [MapTiler Geolocation API](https://docs.maptiler.com/cloud/api/geolocation/).

By utilizing the MapTiler Geolocation API, it becomes possible to dynamically change the language of map labels to match the user’s location main language. This feature enhances the user experience by providing map information in a language that is familiar and easily understood by the visitor. With the MapTiler Geolocation API, businesses and websites can seamlessly integrate this functionality into their mapping applications, ensuring a personalized and localized experience for their users.

## NPM module setup

1.  Install the npm package.
    
    ```bash
    npm install --save @maptiler/sdk
    ```
    
2.  Include the CSS file.
    
    If you have a bundler that can handle CSS, you can `import` the CSS
    
    ```js
    import "@maptiler/sdk/dist/maptiler-sdk.css";
    ```
    
    or include it with a `<link />` in the head of the document via the CDN
    
    ```html
    <link href='https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css' rel='stylesheet' />
    ```
    
3.  Include the following code in your JavaScript file (Example: app.js).
    
    ```js
    import * as maptilersdk from '@maptiler/sdk';
    
     maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
     const map = new maptilersdk.Map({
       container: 'map', // container's id or the HTML element to render the map
       style: maptilersdk.MapStyle.STREETS,
          
       center: [20, 50], // starting position [lng, lat]
       zoom: 4, // starting zoom
          
     });
    ```
    
4.  Replace `YOUR_MAPTILER_API_KEY_HERE` with [your own API key](https://cloud.maptiler.com/account/keys/). Make sure to [secure the key](https://docs.maptiler.com/guides/credentials/api-key/#secure-key) before you publish it.
    
5.  The next is up to you. You can center your map wherever you desire (modifying the `starting position`) and set an appropriate zoom level (modifying the `starting zoom`) to match your users’ needs. Additionally, you can change the map’s look (by updating the `source URL`); choose from a range of visually appealing map styles from our extensive [MapTiler standard maps](https://cloud.maptiler.com/maps/), or create your own to truly differentiate your application.
    
6.  Add event handler for map `load` event. You will add code to change disputed borders according to your map visitor’s location in this handler.
    
    ```js
    map.on('load', async () => {
        
     });
    ```
    
7.  Get the user’s IP country languages codes.
    
    ```js
    map.on('load', async () => {
       const geolocationIP = await maptilersdk.geolocation.info();
       const {country_languages} = geolocationIP;
     });
    ```
    
8.  Change the language of the map. In this example we will use the first language of the language array of the visitor’s country.
    
    ```js
    map.on('load', async () => {
       const geolocationIP = await maptilersdk.geolocation.info();
       const {country_languages} = geolocationIP;
       map.setLanguage(`name:${country_languages[0]}`);
     });
    ```
    

## Basic JavaScript setup

1.  Copy the following code, paste it into your favorite text editor, and save it as a `.html` file.
    
    ```html
    <!DOCTYPE html>
     <html lang="en">
     <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Change the map labels language based on visitor's location</title>
       <script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
       <link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
       <style>
         body { margin: 0; padding: 0; }
         #map { position: absolute; top: 0; bottom: 0; width: 100%; }
       </style>
     </head>
     <body>
       <div id="map"></div>
       <script>
           maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';      
           const map = new maptilersdk.Map({
             container: 'map', // container's id or the HTML element to render the map
             style: maptilersdk.MapStyle.STREETS,
                
             center: [20, 50], // starting position [lng, lat]
             zoom: 4, // starting zoom
                
           });
       </script>
     </body>
     </html>
    ```
    
2.  Replace `YOUR_MAPTILER_API_KEY_HERE` with [your own API key](https://cloud.maptiler.com/account/keys/). Make sure to [secure the key](https://docs.maptiler.com/guides/credentials/api-key/#secure-key) before you publish it.
    
3.  The next is up to you. You can center your map wherever you desire (modifying the `starting position`) and set an appropriate zoom level (modifying the `starting zoom`) to match your users’ needs. Additionally, you can change the map’s look (by updating the `source URL`); choose from a range of visually appealing map styles from our extensive [MapTiler standard maps](https://cloud.maptiler.com/maps/), or create your own to truly differentiate your application.
    
4.  Add event handler for map `load` event. You will add code to change disputed borders according to your map visitor’s location in this handler.
    
    ```js
    map.on('load', async () => {
        
     });
    ```
    
5.  Get the user’s IP country languages codes.
    
    ```js
    map.on('load', async () => {
       const geolocationIP = await maptilersdk.geolocation.info();
       const {country_languages} = geolocationIP;
     });
    ```
    
6.  Change the language of the map. In this example we will use the first language of the language array of the visitor’s country.
    
    ```js
    map.on('load', async () => {
       const geolocationIP = await maptilersdk.geolocation.info();
       const {country_languages} = geolocationIP;
       map.setLanguage(`name:${country_languages[0]}`);
     });
    ```
    

### Complete source code

```html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Change the map labels language based on visitor's location</title>
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
  <script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
  <link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
  <style>
    body { 
      margin: 0;
      padding: 0;
    }

    #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 100%;
    }
  </style>
</head>
<body>
  <div id="map"></div>
  <script>
    maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
    const map = new maptilersdk.Map({
      container: 'map', // container's id or the HTML element to render the map
      style: maptilersdk.MapStyle.STREETS,
      center: [20, 50], // starting position [lng, lat]
      zoom: 4, // starting zoom
    });
    map.on('load', async () => {
      const geolocationIP = await maptilersdk.geolocation.info();
      const {country_languages} = geolocationIP;
      map.setLanguage(`name:${country_languages[0]}`);
    });
  </script>
</body>
</html>
```

## Learn more

Check out the tutorials [How to center map based on visitor’s location](../ip-center-map/), [How to display disputed borders based on visitor’s location](../ip-disputed-borders/) and [How to display the cookies consent bar according to the visitor’s location](../ip-cookie-bar/).

Visit the [MapTiler Geolocation API](https://docs.maptiler.com/cloud/api/geolocation/) reference for Geolocation response object properties. Like for example, the name of the country, the city, etc.

Visit the [MapTiler Planet schema](https://docs.maptiler.com/schema/planet/#place) to know how it is organized into different thematic layers and which attributes and values each layer contains.

Learn more about [Change language in a map](https://docs.maptiler.com/guides/map-design/change-language-in-a-map)

Check out the [How to handle Right-to-Left Texts in Maps](https://docs.maptiler.com/guides/maps-apis/maps-platform/how-to-handle-right-to-left-texts-in-maps)

Read [Disputed borders on your maps](https://docs.maptiler.com/guides/map-design/disputed-borders-on-your-maps)

## Related examples

### [Set map language](https://docs.maptiler.com/sdk-js/examples/language-map/)

Tutorials

This tutorial shows how to change the default map labels language.

### [Geocoding language](https://docs.maptiler.com/sdk-js/examples/geocoding-language/)

Examples

Specify languages for geocoding response text and query weighting.

### [Change a map's language](https://docs.maptiler.com/sdk-js/examples/language-switch/)

Examples

Use setLanguage function to switch languages dynamically.

### [Center map by visitor’s country IP](https://docs.maptiler.com/sdk-js/examples/ip-country-map/)

Tutorials

This tutorial shows how to center map based on the visitor’s country bounds using the MapTiler Geolocation API.
