# Displaying the what3words grid on a Mapbox Map

Adding a Mapbox Map to your website with the what3words grid

Topics: JavaScript, Mapbox, Website

Section: [Display Map](https://developer.what3words.com/docs/display-map.md) > [Display the what3words grid](https://developer.what3words.com/docs/display-map/display-the-what3words-grid.md)

Source: https://developer.what3words.com/tutorial/displaying-the-what3words-grid-on-a-mapbox-map

Updated: 2026-11-27

### Step by step

The [what3words API](https://developer.what3words.com/public-api/docs/#grid-section) supports the ability to obtain a section of the 3m x 3m what3words grid in GeoJSON format, making it very simple to display on a map.

This tutorial will show you how to integrate the what3words grid with Mapbox. We also have tutorials demonstrating its use with both [Google Maps](https://developer.what3words.com/tutorial/displaying-the-what3words-grid-on-a-google-map) and [Leaflet](https://developer.what3words.com/tutorial/displaying-the-what3words-grid-on-a-leafletjs-map).

You will also need to sign up for a Mapbox developer account and be able to view other Mapbox tutorials at [https://docs.mapbox.com/mapbox-gl-js/api/](https://docs.mapbox.com/mapbox-gl-js/api/)

[Grid Section Map Overlay (Mapbox)](https://codesandbox.io/s/grid-section-map-overlay-mapbox-zpdx0) (interactive example)

## 1. Get API key

You will need a what3words API key to complete this tutorial. [Get an API Key](https://accounts.what3words.com/select-plan) or [sign in](https://accounts.what3words.com/).

## 2. Installation

To load the JavaScript wrapper for the what3words API, use a `script` tag like the one in the following example. Replace `YOUR-API-KEY` with your what3words API key to get started.

**Note:** We prefer to use a fixed version of the production version of your integrations. A specific version can also be specified within the script to ensure a predictable version of the component is loaded, for example`@5.0.0`. A log of versions can be found [here.](https://developer.what3words.com/tutorial/javascript-autosuggest-component#versioning)

**JavaScript**

```javascript
<script src="https://cdn.what3words.com/javascript-components@5.0.0/dist/what3words/what3words.js?key=YOUR_API_KEY"></script>
```

The URL contained in the `script` tag is the location of a JavaScript file that loads everything you need for using the JavaScript wrapper for the what3words API.

The `key` parameter contains your application’s API key.

You will also need to load the Mapbox API, for example:

**JavaScript**

```javascript
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.js"></script>
```

## 3. Map style

Within the `head` tag, we need to set style properties for the Mapbox Map. In this example, we’ll set the map to display full screen.

**JavaScript**

```javascript
<link href="https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.css" rel="stylesheet" />
<style>
  #map {
    height: 100%;
  }

  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
</style>
```

## 4. Map initialisation

Within the `body` tag, we need to create a new `div` element to contain the map component, as well as initialise it using the Mapbox JavaScript API.

**JavaScript**

```javascript
<div id="map"></div>
<script>
  // Create the MapBox
  mapboxgl.accessToken = YOUR_MAPBOX_ACCESS_TOKEN;
  let map = new mapboxgl.Map({
    container: "map", // container id
    style: "mapbox://styles/mapbox/streets-v9", // stylesheet location
    center: [-0.195499, 51.52086], // starting position [lng, lat]
    zoom: 18 // starting zoom
  });
  map.addControl(new mapboxgl.NavigationControl());
</script>
```

## 5. Grid overlay

The what3words grid can be displayed as a Mapbox layer by creating a GeoJSON data source, and [attaching it to a new layer](https://docs.mapbox.com/mapbox-gl-js/example/geojson-line/).

Because only a segment of grid section can be requested, we need to obtain a new grid section each time the map is panned or zoomed.

**JavaScript**

```javascript
<script>
  function drawGrid() {
    const zoom = map.getZoom();
    const loadFeatures = zoom > 17;

    if (loadFeatures) { // Zoom level is high enough
      var ne = map.getBounds().getNorthEast();
      var sw = map.getBounds().getSouthWest();

      // Call the what3words Grid API to obtain the grid squares within the current visble bounding box
      what3words.api
        .gridSectionGeoJson({
          southwest: {
            lat: sw.lat, lng: sw.lng
          },
          northeast: {
            lat: ne.lat, lng: ne.lng
          }
        }).then(function(data) {
          // Get the grid source from the map (it won't exist initally)
          var grid = map.getSource('grid');

          if (typeof grid === 'undefined') {
            // Create a source of type 'geojson' which loads the GeoJSON returned from the what3words API
            map.addSource('grid', {
              type: 'geojson',
              data: data
            });

            // Create a new layer, which loads data from the newly created data source
            map.addLayer({
              id: 'grid_layer',
              type: "line",
              source: 'grid',
              layout: {
                "line-join": "round",
                "line-cap": "round"
              },
              paint: {
                "line-color": '#777',
                "line-width": .5
              }
            });
          } else {
            // The source and map layer already exist, so just update the source data to be the new
            // GeoJSON returned from the what3words API
            map.getSource('grid').setData(data);
          }
        }).catch(console.error);
    }

    // If we have reached the required zoom level, set the 'grid_layer' to be visible
    var grid_layer = map.getLayer('grid_layer');
    if (typeof grid_layer !== 'undefined') {
      map.setLayoutProperty('grid_layer', 'visibility', loadFeatures ? 'visible' : 'none');
    }
  }

  // When the map is either loaded or moved, check to see if the grid should be draw
  // if the appropriate zoom level has been met, and if so draw it on.
  map
    .on('load', drawGrid)
    .on('move', drawGrid);
</script>
```

### Full example

The example below takes the step by step guide and puts them into a complete, working example.

**JavaScript**

```javascript
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <script src="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css"
      rel="stylesheet"
    />
    <script src="https://cdn.what3words.com/javascript-components@5.0.0/dist/what3words/what3words.js?key=YOUR-API-KEY-HERE"></script>
    <title>Grid Section Map Overlay (Mapbox)</title>
  </head>
  <body>
    <div id="map" style="width: 100%; height: 400px;"></div>
    <script>
      // Create the Mapbox
      mapboxgl.accessToken =
        "ENTER-YOUR-MAPBOX-TOKEN-HERE";
      let map = new mapboxgl.Map({
        container: "map", // container id
        style: "mapbox://styles/mapbox/streets-v9", // stylesheet location
        center: [-0.195499, 51.52086], // starting position [lng, lat]
        zoom: 18 // starting zoom
      });
      map.addControl(new mapboxgl.NavigationControl());
    </script>
    <script>
      // On map load and move events, invoke the drawGrid function
      map.on("load", drawGrid).on("move", drawGrid);

      // Check to see if the requisit zoom level has been reached, and if so, draw the grid onto the map
      function drawGrid() {
        const zoom = map.getZoom();
        const loadFeatures = zoom > 17;

        if (loadFeatures) {
          // Zoom level is high enough
          var ne = map.getBounds().getNorthEast();
          var sw = map.getBounds().getSouthWest();

          // Call the what3words Grid API to obtain the grid squares within the current visible bounding box
		  what3words.api
            .gridSection({
              boundingBox: {
                southwest: {
                  lat: sw.lat,
                  lng: sw.lng
                },
                northeast: {
                  lat: ne.lat,
                  lng: ne.lng
                }
              },
              format: "geojson"
            })
            .then(function (data) {
              var grid = map.getSource("grid");

              if (grid === undefined) {
                map.addSource("grid", {
                  type: "geojson",
                  data: data
                });
                map.addLayer({
                  id: "grid_layer",
                  type: "line",
                  source: "grid",
                  layout: {
                    "line-join": "round",
                    "line-cap": "round"
                  },
                  paint: {
                    "line-color": "#777",
                    "line-width": 0.5
                  }
                });
              } else {
                map.getSource("grid").setData(data);
              }
            })
            .catch(console.error);
        }

        var grid_layer = map.getLayer("grid_layer");
        if (typeof grid_layer !== "undefined") {
          map.setLayoutProperty(
            "grid_layer",
            "visibility",
            loadFeatures ? "visible" : "none"
          );
        }
      }
    </script>
  </body>
</html>
```
