---
title: "How to display the cookies consent bar according to the visitor's location | JavaScript maps SDK"
description: "This tutorial shows how to display the cookies consent bar according to the visitor's location usign the MapTiler Geolocation API."
---

# How to display the cookies consent bar according to the visitor’s location

In this tutorial, we will demonstrate the process of presenting the cookies consent bar based on the location of the visitor. This can be achieved by utilizing the [MapTiler Geolocation API](https://docs.maptiler.com/cloud/api/geolocation/).

By following the steps outlined in this tutorial, you will be able to implement a solution that dynamically displays the cookie’s consent bar according to the visitor’s specific location. The MapTiler Geolocation API provides the necessary functionality to determine the visitor’s location accurately, allowing you to tailor the display of the cookies consent bar accordingly. By incorporating this feature into your website, you can ensure compliance with relevant regulations and enhance the user experience for visitors from different locations.

## 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: [151.21395, -33.86828], // starting position [lng, lat]
       zoom: 12.18, // 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.  Call [MapTiler’s Geolocation API](https://docs.maptiler.com/cloud/api/geolocation/) to get information about the visitor’s location based on the IP address.
    
    ```js
    (async () => {
       const result = await maptilersdk.geolocation.info();
        
     })();
    ```
    
7.  Get the user’s location country code.
    
    ```js
    (async () => {
       const result = await maptilersdk.geolocation.info();
       const {country_code} = result;
    
     })();
    ```
    
8.  Include the cookie consent bar JavaScript and CSS files in the `head` of your HTML file. To display the cookie consent bar, we will use a third-party library (you can use any library you like or implement your own). In the example, we are using [Osano Cookie Consent](https://www.osano.com/cookieconsent).
    
    ```html
    <script src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"></script>
     <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.css" />
    ```
    
9.  Initialize the cookie consent bar using the user’s country code.
    
    ```js
    (async () => {
       const result = await maptilersdk.geolocation.info();
       const {country_code} = result;
       window.cookieconsent.initialise({
         cookie: {
           domain: "YOUR_DOMAIN"
         },
         palette:{
           popup: {background: "#EFF3FB", text:"#333359", link: "#3170FE"},
           button: {background: "#3170FE"},
         },
         theme: "classic",
         type: "opt-out",
         revokable:true,
         content: {
           href: "https://YOUR_DOMAIN/privacy-policy/"
         },
         law: {
           regionalLaw: false,
           countryCode: country_code
         },
         legal: country_code
       });
     })();
    ```
    

## 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>Display the cookies consent bar according to the 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: [151.21395, -33.86828], // starting position [lng, lat]
             zoom: 12.18, // 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.  Call [MapTiler’s Geolocation API](https://docs.maptiler.com/cloud/api/geolocation/) to get information about the visitor’s location based on the IP address.
    
    ```js
    (async () => {
       const result = await maptilersdk.geolocation.info();
        
     })();
    ```
    
5.  Get the user’s location country code.
    
    ```js
    (async () => {
       const result = await maptilersdk.geolocation.info();
       const {country_code} = result;
    
     })();
    ```
    
6.  Include the cookie consent bar JavaScript and CSS files in the `head` of your HTML file. To display the cookie consent bar, we will use a third-party library (you can use any library you like or implement your own). In the example, we are using [Osano Cookie Consent](https://www.osano.com/cookieconsent).
    
    ```html
    <script src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"></script>
     <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.css" />
    ```
    
7.  Initialize the cookie consent bar using the user’s country code.
    
    ```js
    (async () => {
       const result = await maptilersdk.geolocation.info();
       const {country_code} = result;
       window.cookieconsent.initialise({
         cookie: {
           domain: "YOUR_DOMAIN"
         },
         palette:{
           popup: {background: "#EFF3FB", text:"#333359", link: "#3170FE"},
           button: {background: "#3170FE"},
         },
         theme: "classic",
         type: "opt-out",
         revokable:true,
         content: {
           href: "https://YOUR_DOMAIN/privacy-policy/"
         },
         law: {
           regionalLaw: false,
           countryCode: country_code
         },
         legal: country_code
       });
     })();
    ```
    

### Complete source code

```html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Display the cookies bar according to the 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" />
  <script src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.css" />
  <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: [151.21395, -33.86828], // starting position [lng, lat]
      zoom: 12.18, // starting zoom
    });
    (async () => {
      const result = await maptilersdk.geolocation.info();
      let {country_code} = result;
      country_code = 'ES';
      window.cookieconsent.initialise({
        cookie: {
          domain: "www.maptiler.com"
        },
        palette:{
          popup: {background: "#EFF3FB", text:"#333359", link: "#3170FE"},
          button: {background: "#3170FE"},
        },
        theme: "classic",
        type: "opt-out",
        revokable:true,
        content: {
          href: "https://www.maptiler.com/privacy-policy/"
        },
        law: {
          regionalLaw: false,
          countryCode: country_code
        },
        legal: country_code
      });
    })();
  </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 change the map labels language based on visitor’s location](../ip-map-language/).

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.

## Related examples

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

Tutorials

This tutorial shows how to center map based on the visitor's location using the MapTiler Geolocation API.

### [Map language by IP](https://docs.maptiler.com/sdk-js/examples/ip-map-language/)

Tutorials

This tutorial shows how to automatically change the map labels language based on visitor's location usign the MapTiler Geolocation API.
