# react-leaflet-markercluster

> React wrapper of Leaflet.markercluster for react-leaflet

Latest version **5.0.0-rc.0** (published 2025-01-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-leaflet-markercluster
pnpm add react-leaflet-markercluster
yarn add react-leaflet-markercluster
bun add react-leaflet-markercluster
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 5.0.0-rc.0 |
| Published | 2025-01-03 |
| First published | 2017-01-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 41.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 331 |
| Author | Yevhen Uzhva |
| Maintainers | yuzhva, charlieforward9 |
| Keywords | react, leaflet, react-leaflet, markercluster, marker, cluster |

## Links

- npm: https://www.npmjs.com/package/react-leaflet-markercluster
- Repository: https://github.com/YUzhva/react-leaflet-markercluster
- Homepage: https://github.com/YUzhva/react-leaflet-markercluster#readme
- Issues: https://github.com/YUzhva/react-leaflet-markercluster/issues
- npm.io page: https://npm.io/package/react-leaflet-markercluster

## Dependencies (4)

- [leaflet](https://npm.io/package/leaflet.md) ^1.9.4
- [react-leaflet](https://npm.io/package/react-leaflet.md) ^5.0.0
- [@react-leaflet/core](https://npm.io/package/@react-leaflet/core.md) ^3.0.0
- [leaflet.markercluster](https://npm.io/package/leaflet.markercluster.md) ^1.5.3

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 5.0.0-rc.0 (latest) — 2025-01-03
- 4.2.1 — 2024-12-06
- 4.2.0 — 2024-12-06
- 4.1.2 — 2024-12-06
- 4.1.1 — 2024-11-01
- 4.1.0 — 2024-11-01
- 4.0.0-rc1 — 2024-10-30
- 3.0.0-rc1 — 2020-11-16
- 2.0.0 — 2020-04-03
- 2.0.0-rc3 — 2018-07-10
- 2.0.0-rc2 — 2018-07-09
- 2.0.0-rc1 — 2018-07-08
- 1.1.8 — 2018-03-08
- 1.1.7 — 2017-10-16
- 1.1.6 — 2017-07-10
- … 15 more at https://npm.io/package/react-leaflet-markercluster/versions

## README

# React leaflet markercluster

[![npm](https://img.shields.io/npm/v/react-leaflet-markercluster.svg)](https://www.npmjs.com/package/react-leaflet-markercluster)
[![Code Climate](https://codeclimate.com/github/YUzhva/react-leaflet-markercluster/badges/gpa.svg)](https://codeclimate.com/github/YUzhva/react-leaflet-markercluster)
[![npm](https://img.shields.io/npm/dm/react-leaflet-markercluster.svg)](https://www.npmjs.com/package/react-leaflet-markercluster)
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=plastic)](#license)

React wrapper of [Leaflet.markercluster](https://github.com/Leaflet/Leaflet.markercluster)
for [react-leaflet](https://github.com/PaulLeCam/react-leaflet)

![React leaflet markercluster](./preview.png)

**Examples with the Documentation:** https://yuzhva.github.io/react-leaflet-markercluster/ <br />
**[CodeSandbox Getting Started](https://codesandbox.io/s/react-leaflet-markercluster-getting-started-9binx)**

# Description

If you are faced with an issue with markers or polygons overlapping during map zooming, or they are
overlapping because they are close to each other - you probably need to group them. <br />
That is what you can do with **react-leaflet-markercluster**.

Just grab your markers or [ClusterableRegions](https://github.com/yuzhva/react-leaflet-markercluster/blob/645bcad7b98e0ecf598c99543373aeeb9c8b301f/.storybook/examples/region.jsx#L13-L45) inside `<MarkerClusterGroup />` component, right after `<TileLayer />`:

```javascript
import MarkerClusterGroup from "react-leaflet-markercluster";

<MarkerClusterGroup>
  <Marker position={[49.8397, 24.0297]} />
  <Marker position={[52.2297, 21.0122]} />
  <Marker position={[51.5074, -0.0901]} />
</MarkerClusterGroup>;
```

> **Note: Before getting started, please see these useful guides:**
>
> - [Leaflet Quick Start Guide](http://leafletjs.com/examples/quick-start/).
> - [react-leaflet Installation](https://react-leaflet.js.org/docs/en/installation.html).

# Table of Contents

- [Getting started](#getting-started)
- [API](#api)
- [How to run DEV env](#how-to-run-dev-env)
- [Contributors ✨](#contributors-)
- [Contributing](#contributing)
- [License](#license)

# Getting started

**1.** Install package:

```bash
yarn add react-leaflet-markercluster@next # yarn
npm install react-leaflet-markercluster # npm
```

The `react-leaflet-markercluster` requires `leaflet.markercluster` as [`peerDependency`](https://docs.npmjs.com/files/package.json#peerdependencies)

(Leaflet and react-leaflet also should be installed)

```bash
yarn add leaflet.markercluster leaflet react-leaflet # yarn
npm install leaflet.markercluster leaflet react-leaflet # npm
```

**2.** Import **markercluster** and **leaflet** styles:

```javascript
//sass
@import '~leaflet/dist/leaflet.css'; 
@import '~react-leaflet-markercluster/styles'; 

//cjs
require('~leaflet/dist/leaflet.css');
require('react-leaflet-markercluster/styles'); 

//esm
import 'leaflet/dist/leaflet.css'
import 'react-leaflet-markercluster/styles'
```

Or include CSS styles directly to the head of HTML file:

```html
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />

<link
  rel="stylesheet"
  href="https://unpkg.com/react-leaflet-markercluster/styles"
/>
```

**3.** Write some simple `react-leaflet` Map:

```javascript
import { MapContainer, TileLayer, Marker } from "react-leaflet";

<MapContainer
  className="markercluster-map"
  center={[51.0, 19.0]}
  zoom={4}
  maxZoom={18}
>
  <TileLayer
    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  />

  <Marker position={[49.8397, 24.0297]} />
  <Marker position={[52.2297, 21.0122]} />
  <Marker position={[51.5074, -0.0901]} />
</MapContainer>;
```

**NOTE:** Remember to add map styles `.markercluster-map { height: 90vh; }`.

**4.** Just put your markers or [ClusterableRegions](https://github.com/yuzhva/react-leaflet-markercluster/blob/645bcad7b98e0ecf598c99543373aeeb9c8b301f/.storybook/examples/region.jsx#L13-L45) inside `<MarkerClusterGroup />` component, right after `<TileLayer />`:

```javascript
import MarkerClusterGroup from "react-leaflet-markercluster";

<MarkerClusterGroup>
  <Marker position={[49.8397, 24.0297]} />
  <Marker position={[52.2297, 21.0122]} />
  <Marker position={[51.5074, -0.0901]} />
</MarkerClusterGroup>;
```

[More examples with the Documentation](https://yuzhva.github.io/react-leaflet-markercluster/) <br />
[CodeSandbox Getting Started](https://codesandbox.io/s/react-leaflet-markercluster-getting-started-9binx)

# API

Just pass whatever option you need from [All Leaflet.markercluster Options](https://github.com/Leaflet/Leaflet.markercluster#all-options) to `MarkerClusterGroup` as `prop`.

For example:

```javascript
<MarkerClusterGroup showCoverageOnHover={false} />
```

or:

```javascript
const createClusterCustomIcon = function (cluster) {
  return L.divIcon({
    html: `<span>${cluster.getChildCount()}</span>`,
    className: 'marker-cluster-custom',
    iconSize: L.point(40, 40, true),
  });
}

<MarkerClusterGroup iconCreateFunction={createClusterCustomIcon} />
```

P.S: Examples for v1 are available at [CHANGELOG.md](./CHANGELOG.md#v118)

### Event listeners

You are able to add any listener, supported by Leaflet, with simple `on` property prefix.

# How to run DEV env

**1.** Clone the repo:

```bash
git clone https://github.com/YUzhva/react-leaflet-markercluster.git
```

**2.** Install all dependencies:

```bash
yarn install --no-lockfile # yarn
npm install # npm
```

**3.** Start the server:

```bash
yarn dev # yarn
npm run dev # npm
```

**4.** After starting the server, storybook should automatically open the following address:

```
http://localhost:8080/
```

# Contributors ✨

Thanks goes to these wonderful people:
<a href="https://github.com/yuzhva/react-leaflet-markercluster/graphs/contributors">
<img src="https://contributors-img.web.app/image?repo=yuzhva/react-leaflet-markercluster" />
</a>
Avatars rendered by [contributors-img](https://contributors-img.web.app).

**Special thanks to:**

- [@webcarrot](https://github.com/webcarrot) for [react-leaflet v2 support](https://github.com/yuzhva/react-leaflet-markercluster/issues/71#issuecomment-403071677)
- [@CodeCutterUK](https://github.com/CodeCutterUK) for [react-leaflet v3 support](https://github.com/yuzhva/react-leaflet-markercluster/pull/124)

# Contributing

All sources are placed in the `./src` folder:

**1.** Fork the repo.

**2.** Edit `react-leaflet-markercluster.js` plugin or `style.scss` style files.

**3.** Commit your changes and open Pull Request.

<!--Release instructions https://github.com/yuzhva/react-leaflet-markercluster/pull/210#issuecomment-2447972111 -->

:beer: **Thank you for contribution** :beer:

# UMD

UMD builds are available on [unpkg](https://unpkg.com/):

```html
<!-- unpkg, production code (minified) -->
<script src="https://unpkg.com/react-leaflet-markercluster/dist/index.js"></script>
<!-- unpkg, development code -->
<script src="https://unpkg.com/react-leaflet-markercluster/src/react-leaflet-markercluster.js"></script>

<!-- unpkg, production styles (minified) -->
<link
  rel="stylesheet"
  type="text/css"
  href="https://unpkg.com/react-leaflet-markercluster/styles"
/>
<!-- unpkg, development styles -->
<link
  rel="stylesheet"
  type="text/css"
  href="https://unpkg.com/react-leaflet-markercluster/src/styles.scss"
/>
```

# License

MIT License, see [LICENSE](https://github.com/YUzhva/react-leaflet-markercluster/blob/master/LICENSE) file.

---
_Source: https://npm.io/package/react-leaflet-markercluster · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
