MapSnippets logoMapSnippets logo

Heatmap Visualization with Leaflet

Leaflet

Visualize point density as a heatmap on a Leaflet map using the leaflet.heat plugin.

progress_activityLoading map...
const map = L.map('map').setView([48.8566, 2.3522], 13);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', {
  tileSize: 256,
  attribution: '&copy; <a href="https://www.maptiler.com/">MapTiler</a> &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
const points = [];
for (let i = 0; i < 200; i++) {
  points.push([48.83 + Math.random() * 0.06, 2.28 + Math.random() * 0.15, Math.random()]);
}
L.heatLayer(points, { radius: 20, blur: 15, maxZoom: 17, max: 1.0, gradient: { 0.2: '#0000ff', 0.4: '#00ffff', 0.6: '#00ff00', 0.8: '#ffff00', 1.0: '#ff0000' } }).addTo(map);

How it works

The leaflet.heat plugin renders point data as a smooth heatmap gradient. Each point is an array of [lat, lng, intensity] where intensity (0 to 1) controls the “heat” at that location. Points with no intensity value default to 1.0.

The radius option sets the influence radius in pixels, blur controls smoothing, and maxZoom determines the zoom level at which points reach full intensity. At lower zoom levels, overlapping point radii merge to show hot spots. The heatmap redraws automatically as the user pans and zooms.

Key APIs

API Description Docs
L.heatLayer() Create heatmap layer github.com/Leaflet/Leaflet.heat
radius option Point influence radius (px) github.com/Leaflet/Leaflet.heat

FAQ

How do I update the heatmap data dynamically?

Call heatLayer.setLatLngs(newPoints) to replace all points, or heatLayer.addLatLng(point) to add a single point.

Can I customize the color gradient?

Yes. Pass a gradient object mapping 0-1 values to CSS colors: { gradient: { 0.4: 'blue', 0.6: 'lime', 1: 'red' } }.

Common mistakes

  • Forgetting the third value (intensity): all points will have equal intensity, producing a flat heatmap
  • Setting radius too small: the heatmap may look like individual dots instead of a gradient
  • Loading leaflet-heat.js before leaflet.js: the plugin depends on L being defined
auto_awesome

Build maps using Leaflet with AI

Generate code like this heatmap visualization with leaflet example using the Leaflet skill in your IDE.