Marker clustering
Markers near each other clustered into one badge counting them, split apart as you zoom in.
Map and code
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
These maps come from Google Maps and OpenFreeMap, which see your IP address. They stay off until you say so, once for the whole site. What loading the maps sends
Fetches points-4k.json
<!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><!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><!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><!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><!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>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);<!doctype html>
<!-- The page vanilla.ts runs on: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->Google's bootstrap loader, with your API key: from Google's docs
<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>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);<!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>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>,
);<!doctype html>
<!-- The page react.tsx renders into: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->Google's bootstrap loader, with your API key: from Google's docs
<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>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>,
);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:
clusteron<maplace-map>, on a CDN script that bundles clustering:maplace-element-cluster.iife.json Google, while MapLibre’s script always does (Map attributes).srcnames the points’ file, which the element loads, so the page has no script (Data). - Script and Bundler:
cluster: truein the map’s options, thensetLocationswith the points (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, on MapLibre). - React: the
clusterprop of<MaplaceProvider>(<MaplaceProvider>). - Their look: Cluster badges has the badge’s colours, as Brand colours 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.