Top Related Projects
🔥 JavaScript Library for HTML5 canvas based heatmaps
Marker Clustering plugin for Leaflet
Quick Overview
Leaflet.heat is a plugin for the Leaflet mapping library that adds heatmap functionality. It allows users to create dynamic, interactive heatmaps on top of Leaflet maps, visualizing data density across geographic areas.
Pros
- Easy integration with existing Leaflet projects
- Efficient rendering of large datasets
- Customizable appearance (colors, radius, blur, etc.)
- Supports both canvas and WebGL rendering for performance
Cons
- Limited to 2D heatmaps (no 3D visualization)
- Requires Leaflet as a dependency
- May have performance issues with extremely large datasets
- Limited built-in options for advanced heatmap configurations
Code Examples
- Basic heatmap creation:
var heat = L.heatLayer(data, {radius: 25}).addTo(map);
- Customizing heatmap appearance:
var heat = L.heatLayer(data, {
radius: 20,
blur: 15,
maxZoom: 17,
max: 1.0,
gradient: {0.4: 'blue', 0.65: 'lime', 1: 'red'}
}).addTo(map);
- Adding and removing data points:
heat.addLatLng([50.5, 30.5]);
heat.removeLatLng([50.5, 30.5]);
- Using with GeoJSON data:
var geojsonLayer = L.geoJSON(geojsonData);
var heat = L.heatLayer(geojsonLayer.toGeoJSON().features.map(function(feature) {
return feature.geometry.coordinates.reverse();
})).addTo(map);
Getting Started
- Include Leaflet and Leaflet.heat in your HTML:
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet.heat@0.2.0/dist/leaflet-heat.js"></script>
- Create a map and add a heatmap layer:
var map = L.map('map').setView([37.8, -96], 4);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
var heat = L.heatLayer([
[50.5, 30.5, 0.2], // lat, lng, intensity
[50.6, 30.4, 0.5],
[50.7, 30.3, 0.8]
], {radius: 25}).addTo(map);
Competitor Comparisons
🔥 JavaScript Library for HTML5 canvas based heatmaps
Pros of heatmap.js
- More flexible and can be used with various map libraries or standalone
- Offers more customization options for heatmap appearance
- Supports both canvas and WebGL rendering for better performance
Cons of heatmap.js
- Requires more setup and configuration compared to Leaflet.heat
- May have a steeper learning curve for beginners
- Not specifically optimized for Leaflet, which could lead to integration challenges
Code Comparison
heatmap.js:
var heatmapInstance = h337.create({
container: document.querySelector('.heatmap'),
radius: 10,
maxOpacity: .8,
});
heatmapInstance.setData({
max: 5,
data: [{x: 10, y: 15, value: 5}]
});
Leaflet.heat:
var heat = L.heatLayer([
[50.5, 30.5, 0.2],
[50.6, 30.4, 0.5],
[50.7, 30.3, 0.8]
], {radius: 25}).addTo(map);
The code comparison shows that heatmap.js requires more setup but offers more customization, while Leaflet.heat is more straightforward to use within Leaflet maps. heatmap.js uses a create method with various options, whereas Leaflet.heat is implemented as a Leaflet layer with simpler initialization.
Marker Clustering plugin for Leaflet
Pros of Leaflet.markercluster
- Efficiently handles large numbers of markers by clustering them
- Provides better performance and less visual clutter for dense datasets
- Offers customizable cluster appearance and behavior
Cons of Leaflet.markercluster
- More complex to implement compared to simple heat maps
- May not be suitable for continuous data representation
- Requires additional processing to create clusters
Code Comparison
Leaflet.heat:
var heat = L.heatLayer(points, {radius: 25}).addTo(map);
Leaflet.markercluster:
var markers = L.markerClusterGroup();
markers.addLayer(L.marker(getRandomLatLng(map)));
map.addLayer(markers);
Leaflet.heat is simpler to implement for visualizing point density, while Leaflet.markercluster offers more control over marker grouping and interaction. Leaflet.heat is better suited for continuous data representation, whereas Leaflet.markercluster excels at displaying discrete point data with additional information on click or hover events. The choice between the two depends on the specific data visualization needs and the desired level of user interaction.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Leaflet.heat
A tiny, simple and fast Leaflet heatmap plugin. Uses simpleheat under the hood, additionally clustering points into a grid for performance.
Demos
Basic Usage
var heat = L.heatLayer([
[50.5, 30.5, 0.2], // lat, lng, intensity
[50.6, 30.4, 0.5],
...
], {radius: 25}).addTo(map);
To include the plugin, just use leaflet-heat.js
from the dist
folder:
<script src="leaflet-heat.js"></script>
Building
To build the dist files run:
npm install && npm run prepublish
Reference
L.heatLayer(latlngs, options)
Constructs a heatmap layer given an array of points and an object with the following options:
- minOpacity - the minimum opacity the heat will start at
- maxZoom - zoom level where the points reach maximum intensity (as intensity scales with zoom),
equals
maxZoom
of the map by default - max - maximum point intensity,
1.0
by default - radius - radius of each "point" of the heatmap,
25
by default - blur - amount of blur,
15
by default - gradient - color gradient config, e.g.
{0.4: 'blue', 0.65: 'lime', 1: 'red'}
- pane - Map pane where the heat will be drawn. Defaults to 'overlayPane'.
Each point in the input array can be either an array like [50.5, 30.5, 0.5]
,
or a Leaflet LatLng object.
Optional third argument in each LatLng
point (altitude
) represents point intensity.
Unless max
option is specified, intensity should range between 0.0
and 1.0
.
Methods
- setOptions(options): Sets new heatmap options and redraws it.
- addLatLng(latlng): Adds a new point to the heatmap and redraws it.
- setLatLngs(latlngs): Resets heatmap data and redraws it.
- redraw(): Redraws the heatmap.
Changelog
0.2.0 — Oct 26, 2015
- Fixed intensity to work properly with
max
option. - Fixed zoom animation on Leaflet 1.0 beta 2.
- Fixed tiles and point intensity in demos.
0.1.3 — Nov 25, 2015
- Fixed some edge cases when handling point intensity.
- Added
minOpacity
option.
0.1.2 — Nov 5, 2014
- Added compatibility with Leaflet 0.8-dev.
0.1.1 — Apr 22, 2014
- Fixed overlaying two heatmaps on top of each other.
- Fixed rare animation issues.
0.1.0 — Feb 3, 2014
- Added
addLatLng
,setLatlngs
,setOptions
andredraw
methods. - Added
max
option and support for different point intensity values (throughLatLng
third argument). - Added
gradient
option to customize colors.
0.0.1 — Jan 31, 2014
- Initial release.
Top Related Projects
🔥 JavaScript Library for HTML5 canvas based heatmaps
Marker Clustering plugin for Leaflet
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot