How to create a heatmap layer in Leaflet
Learn how to create a heatmap layer in Leaflet JavaScript library using the leaflet-maptilersdk plugin.
In this example, the heatmap layer is zoom-compensated and as a result the blobs roughly maintain their size in world-scale as we zoom-in/out. Download the earthquake sample data.
NPM module setup
npm install --save @maptiler/leaflet-maptilersdk leaflet
import { MaptilerLayer, MapStyle } from '@maptiler/leaflet-maptilersdk';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
const map = L.map('map').setView([37.999, -119.715], 7);
const mtLayer = new MaptilerLayer({
apiKey: 'YOUR_MAPTILER_API_KEY_HERE',
style: MapStyle.DATAVIZ.LIGHT, // optional
}).addTo(map);
mtLayer.on("ready", () => {
// Leverage the MapTiler SDK layer helpers to easily add polyline / point / polygon / heatmap layers
mtLayer.addHeatmap({
data: 'earthquakes.geojson',
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>How to create a heatmap layer in Leaflet | NPM example</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="map"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
#map {position: absolute; top: 0; right: 0; bottom: 0; left: 0;}
Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.
Learn more
Check out the Heatmap Layer Helper documentation and look at the Heatmap Helper examples to see the visualization possibilities.
Related examples
Display thousands of lines in Leaflet
ExamplesDisplay thousands of lines efficiently in Leaflet using maptilersdk.
Display thousands of polygons in Leaflet
ExamplesDisplay thousands of polygons efficiently in Leaflet using maptilersdk.
Plotting millions of points in Leaflet
ExamplesDisplay millions of points efficiently in Leaflet using maptilersdk.