React Leaflet Markercluster

5.0.0-rc.0 · active · verified Sun Apr 19

react-leaflet-markercluster is a dedicated React component that provides an intuitive wrapper for the popular Leaflet.markercluster plugin, designed specifically for use within react-leaflet applications. It addresses the common challenge of visualizing a large number of markers on a map by dynamically grouping them into clusters at various zoom levels, preventing clutter and improving map performance and user experience. The current stable release is v4.2.1, with v5.0.0-rc.0 currently in a release candidate phase to support React 19 and react-leaflet v5. The package aims for stability and compatibility with major react-leaflet versions, often releasing new major versions to align with its peer dependencies. Its primary differentiator is its seamless integration into the react-leaflet ecosystem, allowing developers to simply wrap existing Leaflet Marker components within a `<MarkerClusterGroup />` to enable clustering functionality without complex imperative Leaflet API calls, while also exposing full control over Leaflet.markercluster options via component props.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic Leaflet map with `react-leaflet` and enable marker clustering using `react-leaflet-markercluster`, showing multiple markers that group dynamically on zoom.

import React from 'react';
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
import MarkerClusterGroup from 'react-leaflet-markercluster';

// Ensure Leaflet and MarkerClusterGroup styles are loaded
import 'leaflet/dist/leaflet.css';
import 'react-leaflet-markercluster/styles';

function MyMapComponent() {
  const markers = [
    { position: [49.8397, 24.0297], id: 1, name: 'Lviv' },
    { position: [52.2297, 21.0122], id: 2, name: 'Warsaw' },
    { position: [51.5074, -0.0901], id: 3, name: 'London' },
    { position: [50.0880, 14.4208], id: 4, name: 'Prague' },
    { position: [48.8566, 2.3522], id: 5, name: 'Paris' },
    { position: [40.7128, -74.0060], id: 6, name: 'New York' },
    { position: [34.0522, -118.2437], id: 7, name: 'Los Angeles' },
    { position: [35.6895, 139.6917], id: 8, name: 'Tokyo' }
  ];

  return (
    <MapContainer
      className="markercluster-map"
      center={[51.0, 19.0]}
      zoom={4}
      maxZoom={18}
      style={{ height: '90vh', width: '100%' }} // Inline style for quick demo
    >
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      />

      <MarkerClusterGroup chunkedLoading>
        {markers.map((marker) => (
          <Marker key={marker.id} position={marker.position}>
            <Popup>{marker.name}</Popup>
          </Marker>
        ))}
      </MarkerClusterGroup>
    </MapContainer>
  );
}

export default MyMapComponent;

view raw JSON →