deckgl-heatmap

Sample code supporting Google Maps Platform JavaScript API documentation. author(s): Geo Developer IX Documentation Team

HTML

<!doctype html>
<!--
 @license
 Copyright 2019 Google LLC. All Rights Reserved.
 SPDX-License-Identifier: Apache-2.0
-->

<html>
  <head>
    <title>deck.gl HeatmapLayer and Google Maps Platform</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!-- Use Material Design Progress indicator -->
    <link
      href="https://unpkg.com/material-components-web@6.0.0/dist/material-components-web.css"
      rel="stylesheet" />
    <script src="https://unpkg.com/material-components-web@6.0.0/dist/material-components-web.min.js"></script>
    <script src="https://unpkg.com/deck.gl@8.9.22/dist.min.js"></script>
    <script src="https://unpkg.com/@deck.gl/google-maps@8.9.22/dist.min.js"></script>

    <script>
      // prettier-ignore
      (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
            });
    </script>
  </head>
  <body>
    <div
      role="progressbar"
      class="mdc-linear-progress"
      aria-label="Data Progress Bar">
      <div class="mdc-linear-progress__buffer">
        <div class="mdc-linear-progress__buffer-bar"></div>
        <div class="mdc-linear-progress__buffer-dots"></div>
      </div>
      <div class="mdc-linear-progress__bar mdc-linear-progress__primary-bar">
        <span class="mdc-linear-progress__bar-inner"></span>
      </div>
      <div class="mdc-linear-progress__bar...

CSS

/*
 * @license
 * Copyright 2025 Google LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

/*
 * Always set the map height explicitly to define the size of the div element
 * that contains the map.
 */
#map {
  height: 100%;
  flex-grow: 1; /* Make map take up remaining space */
}

/*
 * Optional: Makes the sample page fill the window.
 */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  display: flex; /* Use flexbox for layout */
  flex-direction: column; /* Stack children vertically */
  position: relative; /* Set body as positioning context */
  font-family: 'Roboto', Arial, sans-serif; /* Set font family */
}

#toggleButton {
  position: absolute;
  top: 70px; /* Position towards the top of the map area */
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000; /* Ensure it's above the map */
}

#tooltip {
  position: absolute;
  z-index: 1001; /* Ensure it's above the button and map */
  padding: 10px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  border-radius: 4px;
  pointer-events: none; /* Allows interaction with elements behind the tooltip */
  display: none; /* Hidden by default */
  font-size: 12px;
}

h1 {
  text-align: center;
  margin: 10px;
}

#legend {
  position: absolute;
  top: 90px; /* Adjust position as needed */
  right: 10px; /* Position on the left */
  z-index: 1000; /* Ensure it's above the map */
  background-color: rgba(255, 255, 255, 0.9);
  padding: 10px;
  border-radius: 4px;
  font-size: 14px;
}

#legend h4 {
  margin-top: 0;
  margin-bottom: 5px;
}

#legend div {
  display: flex;
  align-items: center;
  margin-bottom: 3px;
}

#legend span {
  display: inline-block;
  width: 20px;
  height: 14px;
  margin-right: 5px;
  border: 1px solid #000; /* Add a border for visibility */
}

/* Media query for mobile devices */
@media (max-width: 600px) {
  #legend {
    position: absolute;
    bottom: 50px; /* Position above the button */
    left: 50%; /* Center horizontally */
    transform:...

JavaScript

'use strict';
/*
 * @license
 * Copyright 2025 Google LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

// Initialize and add the map
let map;
// Use global types for Deck.gl components
let heatmapLayer;
let googleMapsOverlay;
let marker;
let infoWindow;

async function init() {
  // Progress bar logic moved from index.html
  let progress;
  const progressDiv = document.querySelector('.mdc-linear-progress');
  if (progressDiv) {
    // Assuming 'mdc' is globally available, potentially loaded via a script tag
    // If not, you might need to import it or add type definitions.
    progress = new mdc.linearProgress.MDCLinearProgress(progressDiv);
    progress.open();
    progress.determinate = false;
    progress.done = function () {
      progress.close();
      progressDiv.remove();
    };
  }

  // The location for the map center.
  const position = { lat: 37.77325660358167, lng: -122.41712341793448 }; // Using the center from original deckgl-polygon.js

  //  Request needed libraries.
  const [{ Map, InfoWindow }, { AdvancedMarkerElement }] = await Promise.all([
    google.maps.importLibrary('maps'),
    google.maps.importLibrary('marker'),
  ]);

  const mapDiv = document.getElementById('map');
  if (!mapDiv) {
    console.error('Map element not found!');
    return;
  }

  // The map, centered at the specified position
  map = new Map(mapDiv, {
    zoom: 13, // Using the zoom from original deckgl-polygon.js
    center: position,
    tilt: 90, // Add tilt
    heading: -25, // Add heading
    mapId: '6b73a9fe7e831a00',
    fullscreenControl: false, // Disable fullscreen control
    clickableIcons: false, // Disable clicks on base map POIs
  });

  // Deck.gl Layer and Overlay
  // Use global deck object
  heatmapLayer = new deck.HeatmapLayer({
    // Assign to the outer heatmapLayer
    id: 'HeatmapLayer', // Change layer ID
    data: 'https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/sf-bike-parking.json', // Use the loaded...