Visualize points as cluster maps in Leaflet

This tutorial shows how to visualize points as cluster maps with Leaflet JS. Learn how to group data points into clusters for effective data analysis and visualization.

In this Leaflet example, we use the Leaflet.markercluster plugin to create a cluster map with smooth animations and high-performance.

NPM module setup


npm install --save @maptiler/leaflet-maptilersdk leaflet leaflet.markercluster
import { MaptilerLayer, MapStyle } from '@maptiler/leaflet-maptilersdk';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import L from 'leaflet.markercluster';
import 'leaflet.markercluster/dist/MarkerCluster.css';
import 'leaflet.markercluster/dist/MarkerCluster.Default.css';

const key = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new L.Map('map', {maxZoom: 22}).setView([0, 20], 1);
const mtLayer = new MaptilerLayer({
  style: MapStyle.DATAVIZ.LIGHT,
  apiKey: key,
}).addTo(map);

const markers = L.markerClusterGroup();

(async () => {
  const response = await fetch('https://docs.maptiler.com/sdk-js/assets/earthquakes.geojson');
  const jsonData = await response.json();

  for (let i = 0; i < jsonData.features.length; i++) {
    const feature = jsonData.features[i];
    const title = feature.properties.mag;
    const marker = L.marker(new L.LatLng(feature.geometry.coordinates[1], feature.geometry.coordinates[0]), { title: title });
    marker.bindPopup(`${title}`);
    markers.addLayer(marker);
  }

  map.addLayer(markers);
})();

Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.

Plotting thousands or millions of points in Leaflet

Plotting millions of points in Leaflet

Examples

Display millions of points efficiently in Leaflet using maptilersdk.

How to create a heatmap layer in Leaflet

How to create a heatmap layer in Leaflet

Examples

Create heatmap layers in Leaflet using the maptilersdk plugin.

Was this helpful?