All tutorials

Displaying the what3words grid on a LeafletJS Map


The what3words API 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 LeafletJS. We also have tutorials demonstrating its use with both Mapbox and Google Maps.

LeafletJS is an open source JavaScript mapping engine. More information can be found at


You will need a what3words API key to complete this tutorial.



To load the JavaScript wrapper for the what3words API, use a script tag like the one in the following example:

<script src=""></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 LeafletJS API, for example:

<script src="" integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" crossorigin=""></script>

Map style

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

<link rel="stylesheet" href="" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin="" />
  #map {
    height: 100%;

  html, body {
    height: 100%;
    margin: 0;
    padding: 0;

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 LeafletJS API.

<div id="map"></div>
  var map ='map').setView([51.505, -0.09], 19);
  L.tileLayer('https://{s}{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="">OpenStreetMap</a> contributors',

Grid overlay

The what3words grid can be displayed on top of a LeafletJS Map adding a GeoJSON layer to the map.

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.

  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
          southwest: {
            lng: sw.lng
          northeast: {
            lng: ne.lng
        }).then(function(data) {
          // If the grid layer is already present, remove it as it will need to be replaced by the new grid section
          if (typeof grid_layer !== 'undefined') {

          // Create a new GeoJSON layer, based on the GeoJSON returned from the what3words API
          grid_layer = L.geoJSON(data, {
            style: function() {
              return {
                color: '#777',
                stroke: true,
                weight: 0.5
    } else {
      // If the grid layer already exists, remove it as the zoom level no longer requires the grid to be displayed
      if (typeof grid_layer !== 'undefined') {

  map.on('move', drawGrid);

Related tutorials

Back to top