Toggle deck.gl layer

This example shows how to toggle a deck.gl layer using MapTiler SDK JS.

NPM module setup


npm install --save @maptiler/sdk deck.gl
import { Map, MapStyle, config, Popup } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';
import { ScatterplotLayer, MapboxOverlay } from 'deck.gl';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
  container: 'map',
  style: MapStyle.DATAVIZ.LIGHT,
  center: [2.345885, 48.860412],
  zoom: 12,
});

// 5 Beautiful gardens in Paris
const sampleData = {
  type: 'FeatureCollection',
  name: 'Jardins Des Paris',
  crs: {
    type: 'name',
    properties: { name: 'urn:ogc:def:crs:OGC:1.3:CRS84' },
  },
  features: [
    {
      type: 'Feature',
      properties: {
        name: 'Jardins du Trocadéro',
        district: 16,
      },
      geometry: {
        type: 'Point',
        coordinates: [2.289207, 48.861561],
      },
    },
    {
      type: 'Feature',
      properties: {
        name: 'Jardin des Plantes',
        district: 5,
      },
      geometry: {
        type: 'Point',
        coordinates: [2.359823, 48.843995],
      },
    },
    {
      type: 'Feature',
      properties: {
        name: 'Jardins das Tulherias',
        district: 1,
      },
      geometry: {
        type: 'Point',
        coordinates: [2.327092, 48.863608],
      },
    },
    {
      type: 'Feature',
      properties: {
        name: 'Parc de Bercy',
        district: 12,
      },
      geometry: {
        type: 'Point',
        coordinates: [2.382094, 48.835962],
      },
    },
    {
      type: 'Feature',
      properties: {
        name: 'Jardin du Luxemburg',
        district: 6,
      },
      geometry: {
        type: 'Point',
        coordinates: [2.336975, 48.846421],
      },
    },
  ],
};

let isHovering = false;

// Add the overlay as a control
function initializeOverlay() {
  const layer = new ScatterplotLayer({
    id: 'scatterplot-layer',
    data: sampleData.features,
    pickable: true,
    autoHighlight: true,
    opacity: 0.8,
    stroked: true,
    filled: true,
    radiusScale: 6,
    radiusMinPixels: 20,
    radiusMaxPixels: 100,
    lineWidthMinPixels: 5,
    getPosition: (d) => d.geometry.coordinates,
    getFillColor: (d) => [49, 130, 206],
    getLineColor: (d) => [175, 0, 32],
    onClick: (info) => {
      const { coordinate, object } = info;
      const description = `<div>
        <p>
          <strong>Name: </strong>${object.properties['name']}
        </p>
          <strong>Disctrict: </strong>${object.properties['district']}
        </p>
      </div>`;

      new Popup()
        .setLngLat(coordinate)
        .setHTML(description)
        .addTo(map);
    },
  });

  // Create the overlay
  overlay = new MapboxOverlay({
    layers: [layer],
  });

  map.addControl(overlay);
}

let show = true; // Display the layer by default

map.on('load', () => {
  // Add the overlay
  initializeOverlay();

  const toggleButton = document.querySelector('#toggle-button');
  toggleButton.addEventListener('click', () => {
    if (show) {
      map.removeControl(overlay);
      toggleButton.innerText = 'Show';
      show = false;
    } else {
      initializeOverlay();
      toggleButton.innerText = 'Hide';
      show = true;
    }
  });
});

Learn more

Check out Deck.gl with MapTiler maps for more examples of integrating Deck.gl layers into MapTiler maps.

Filter symbols by toggling a list

Filter symbols by toggling a list

Examples

Filter symbols based on property values in the dataset.

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

Map language by IP

Tutorials

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

How to display a map legend control to toggle layers visualization

Legend control

Tutorials

Display a map legend control to toggle layer visibility.

How to display a style switcher control

Style switcher

Tutorials

Add a style switcher control to your map.

Was this helpful?