# Marker clustering

Markers near each other clustered into one badge counting them, split apart as you zoom in.

## How it works

A large set of points from a JSON file: those near each other are drawn as one badge counting them, so the map stays fast and readable.

- **HTML**: `cluster` on `<maplace-map>`, on a CDN script that bundles clustering: `maplace-element-cluster.iife.js` on Google, while MapLibre's script always does ([Map attributes](/element/#map-attributes)). `src` names the points' file, which the element loads, so the page has no script ([Data](/element/#data)).
- **Script** and **Bundler**: `cluster: true` in the map's options, then [`setLocations`](/store/#methods) with the points ([Options](/store/#options)). The Script version on Google is on the same cluster script; from npm, each provider loads its clustering only for a map that clusters ([on Google](/google/#clustering), [on MapLibre](/maplibre/#clustering)).
- **React**: the `cluster` prop of `<MaplaceProvider>` ([`<MaplaceProvider>`](/react/#maplaceprovider)).
- **Their look**: [Cluster badges](/styling/#cluster-badges) has the badge's colours, as [Brand colours](/examples/styling/brand/) uses them.
- **With a list**: a list of every point is too long to use; one that lists only what is in the map's view is in [Search this area](/examples/search/this-area/).

## Code

### HTML on MapLibre

`packages/maplace/examples/big/maplibre/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- Clustered points from a JSON file, which the element loads from src (the MapLibre build bundles its clustering): the markers near each other drawn as one badge counting them, which splits as you zoom in. -->
<maplace-map id="big-maplibre-map" cluster src="/examples/data/points-4k.json"></maplace-map>
```

### HTML on Google

`packages/maplace/examples/big/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-cluster.iife.js"></script>
<!-- Clustered points from a JSON file, which the element loads from src (the cluster build bundles @googlemaps/markerclusterer): the markers near each other drawn as one badge counting them, which splits as you zoom in. -->
<maplace-map id="big-map" api-key="YOUR_KEY" cluster src="/examples/data/points-4k.json"></maplace-map>
```

### Script on MapLibre

`packages/maplace/examples/big/maplibre/script.html`

```html
<!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl, its stylesheet and its clustering, what the map mounts on, and the map made with Maplace; its points come from points-4k.json. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
  #big-maplibre-script-map { height: 24rem; }
</style>
<div id="big-maplibre-script-map"></div>
<script type="module">
  const maplace = Maplace.createMapLibreMap("#big-maplibre-script-map", {
    cluster: true,
  });

  const response = await fetch("/examples/data/points-4k.json");
  const points = await response.json();
  maplace.setLocations(points);
</script>
```

### Script on Google

`packages/maplace/examples/big/script.html`

```html
<!doctype html>
<!-- A page of your own script: the cluster CDN script, which bundles @googlemaps/markerclusterer, what the map mounts on, and the map made with Maplace, which adds Maps JS with your key; its points come from points-4k.json. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-cluster.iife.js"></script>
<style>
  #big-script-map { height: 24rem; }
</style>
<div id="big-script-map"></div>
<script type="module">
  const maplace = Maplace.createGoogleMap("#big-script-map", {
    apiKey: "YOUR_KEY",
    cluster: true,
  });

  const response = await fetch("/examples/data/points-4k.json");
  const points = await response.json();
  maplace.setLocations(points);
</script>
```

### Bundler on MapLibre

`packages/maplace/examples/big/maplibre/vanilla.html`

```html
<!doctype html>
<!-- The page vanilla.ts runs on: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
  #big-maplibre-vanilla-map { height: 24rem; }
</style>
<div id="big-maplibre-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/big/maplibre/vanilla.ts`

```ts
import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";

const maplace = createMapLibreMap("#big-maplibre-vanilla-map", {
  cluster: true,
});

const response = await fetch("/examples/data/points-4k.json");
const points: Location[] = await response.json();
maplace.setLocations(points);
```

### Bundler on Google

`packages/maplace/examples/big/vanilla.html`

```html
<!doctype html>
<!-- The page vanilla.ts runs on: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->
<script>
  (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
    key: "YOUR_KEY",
    v: "weekly",
  });
</script>
<style>
  #big-vanilla-map { height: 24rem; }
</style>
<div id="big-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/big/vanilla.ts`

```ts
import type { Location } from "@maplacejs/maplace";
import { createGoogleMap } from "@maplacejs/maplace/google";

const maplace = createGoogleMap("#big-vanilla-map", {
  cluster: true,
});

const response = await fetch("/examples/data/points-4k.json");
const points: Location[] = await response.json();
maplace.setLocations(points);
```

### React on MapLibre

`packages/maplace/examples/big/maplibre/react.html`

```html
<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
  #big-maplibre-react-root .map { height: 24rem; }
</style>
<div id="big-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/big/maplibre/react.tsx`

```tsx
import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";

function Big() {
  const [points, setPoints] = useState<readonly Location[]>([]);
  useEffect(() => {
    fetch("/examples/data/points-4k.json")
      .then((response) => response.json())
      .then(setPoints);
  }, []);
  return (
    <MaplaceProvider locations={points} cluster>
      <MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("big-maplibre-react-root") as HTMLElement).render(
  <StrictMode>
    <Big />
  </StrictMode>,
);
```

### React on Google

`packages/maplace/examples/big/react.html`

```html
<!doctype html>
<!-- The page react.tsx renders into: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->
<script>
  (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
    key: "YOUR_KEY",
    v: "weekly",
  });
</script>
<style>
  #big-react-root .map { height: 24rem; }
</style>
<div id="big-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/big/react.tsx`

```tsx
import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";

function Big() {
  const [points, setPoints] = useState<readonly Location[]>([]);
  useEffect(() => {
    fetch("/examples/data/points-4k.json")
      .then((response) => response.json())
      .then(setPoints);
  }, []);
  return (
    <MaplaceProvider locations={points} cluster>
      <MaplaceMap createAdapter={createGoogleAdapter} className="map" />
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("big-react-root") as HTMLElement).render(
  <StrictMode>
    <Big />
  </StrictMode>,
);
```
