code Snippetlanguage CDNpackage_2 NPM
content_copy Copyopen_in_new Open
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 15.5 , 49.8 ],
zoom: 6
});
const pts = Array. from ({ length: 300 }, () => ({
type: 'Feature' ,
geometry: { type: 'Point' , coordinates: [ 12 + Math. random () * 7 , 48.5 + Math. random () * 2.5 ] }
}));
map. on ( 'load' , () => {
map. addSource ( 'pts' , { type: 'geojson' , data: { type: 'FeatureCollection' , features: pts }, cluster: true , clusterMaxZoom: 14 , clusterRadius: 50 });
map. addLayer ({ id: 'clusters' , type: 'circle' , source: 'pts' , filter: [ 'has' , 'point_count' ],
paint: { 'circle-color' : [ 'step' , [ 'get' , 'point_count' ], '#51bbd6' , 50 , '#f1f075' , 200 , '#f28cb1' ], 'circle-radius' : [ 'step' , [ 'get' , 'point_count' ], 18 , 50 , 25 , 200 , 35 ] } });
map. addLayer ({ id: 'cluster-count' , type: 'symbol' , source: 'pts' , filter: [ 'has' , 'point_count' ],
layout: { 'text-field' : '{point_count_abbreviated}' , 'text-font' : [ 'Noto Sans Regular' ], 'text-size' : 12 } });
map. addLayer ({ id: 'single' , type: 'circle' , source: 'pts' , filter: [ '!' , [ 'has' , 'point_count' ]],
paint: { 'circle-color' : '#11b4da' , 'circle-radius' : 6 , 'circle-stroke-width' : 1 , 'circle-stroke-color' : '#fff' } });
map. on ( 'click' , 'clusters' , ( e ) => {
const cid = e.features[ 0 ].properties.cluster_id;
map. getSource ( 'pts' ). getClusterExpansionZoom (cid, ( err , z ) => { if ( ! err) map. easeTo ({ center: e.features[ 0 ].geometry.coordinates, zoom: z }); });
});
}); content_copy Copyopen_in_new Open
<! DOCTYPE html >
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" />
< link rel = "stylesheet" href = "https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.css" />
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< script type = "module" >
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.mjs' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 15.5 , 49.8 ],
zoom: 6
});
const pts = Array. from ({ length: 300 }, () => ({
type: 'Feature' ,
geometry: { type: 'Point' , coordinates: [ 12 + Math. random () * 7 , 48.5 + Math. random () * 2.5 ] }
}));
map. on ( 'load' , () => {
map. addSource ( 'pts' , { type: 'geojson' , data: { type: 'FeatureCollection' , features: pts }, cluster: true , clusterMaxZoom: 14 , clusterRadius: 50 });
map. addLayer ({ id: 'clusters' , type: 'circle' , source: 'pts' , filter: [ 'has' , 'point_count' ],
paint: { 'circle-color' : [ 'step' , [ 'get' , 'point_count' ], '#51bbd6' , 50 , '#f1f075' , 200 , '#f28cb1' ], 'circle-radius' : [ 'step' , [ 'get' , 'point_count' ], 18 , 50 , 25 , 200 , 35 ] } });
map. addLayer ({ id: 'cluster-count' , type: 'symbol' , source: 'pts' , filter: [ 'has' , 'point_count' ],
layout: { 'text-field' : '{point_count_abbreviated}' , 'text-font' : [ 'Noto Sans Regular' ], 'text-size' : 12 } });
map. addLayer ({ id: 'single' , type: 'circle' , source: 'pts' , filter: [ '!' , [ 'has' , 'point_count' ]],
paint: { 'circle-color' : '#11b4da' , 'circle-radius' : 6 , 'circle-stroke-width' : 1 , 'circle-stroke-color' : '#fff' } });
map. on ( 'click' , 'clusters' , ( e ) => {
const cid = e.features[ 0 ].properties.cluster_id;
map. getSource ( 'pts' ). getClusterExpansionZoom (cid, ( err , z ) => { if ( ! err) map. easeTo ({ center: e.features[ 0 ].geometry.coordinates, zoom: z }); });
});
});
</ script >
</ body >
</ html > content_copy Copyopen_in_new Open
import maplibregl from 'maplibre-gl' ;
import 'maplibre-gl/dist/maplibre-gl.css' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 15.5 , 49.8 ],
zoom: 6
});
const pts = Array. from ({ length: 300 }, () => ({
type: 'Feature' ,
geometry: { type: 'Point' , coordinates: [ 12 + Math. random () * 7 , 48.5 + Math. random () * 2.5 ] }
}));
map. on ( 'load' , () => {
map. addSource ( 'pts' , { type: 'geojson' , data: { type: 'FeatureCollection' , features: pts }, cluster: true , clusterMaxZoom: 14 , clusterRadius: 50 });
map. addLayer ({ id: 'clusters' , type: 'circle' , source: 'pts' , filter: [ 'has' , 'point_count' ],
paint: { 'circle-color' : [ 'step' , [ 'get' , 'point_count' ], '#51bbd6' , 50 , '#f1f075' , 200 , '#f28cb1' ], 'circle-radius' : [ 'step' , [ 'get' , 'point_count' ], 18 , 50 , 25 , 200 , 35 ] } });
map. addLayer ({ id: 'cluster-count' , type: 'symbol' , source: 'pts' , filter: [ 'has' , 'point_count' ],
layout: { 'text-field' : '{point_count_abbreviated}' , 'text-font' : [ 'Noto Sans Regular' ], 'text-size' : 12 } });
map. addLayer ({ id: 'single' , type: 'circle' , source: 'pts' , filter: [ '!' , [ 'has' , 'point_count' ]],
paint: { 'circle-color' : '#11b4da' , 'circle-radius' : 6 , 'circle-stroke-width' : 1 , 'circle-stroke-color' : '#fff' } });
map. on ( 'click' , 'clusters' , ( e ) => {
const cid = e.features[ 0 ].properties.cluster_id;
map. getSource ( 'pts' ). getClusterExpansionZoom (cid, ( err , z ) => { if ( ! err) map. easeTo ({ center: e.features[ 0 ].geometry.coordinates, zoom: z }); });
});
});
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [15.5, 49.8],
zoom: 6
});
const pts = Array.from({ length: 300 }, () => ({
type: 'Feature',
geometry: { type: 'Point', coordinates: [12 + Math.random() * 7, 48.5 + Math.random() * 2.5] }
}));
map.on('load', () => {
map.addSource('pts', { type: 'geojson', data: { type: 'FeatureCollection', features: pts }, cluster: true, clusterMaxZoom: 14, clusterRadius: 50 });
map.addLayer({ id: 'clusters', type: 'circle', source: 'pts', filter: ['has', 'point_count'],
paint: { 'circle-color': ['step', ['get', 'point_count'], '#51bbd6', 50, '#f1f075', 200, '#f28cb1'], 'circle-radius': ['step', ['get', 'point_count'], 18, 50, 25, 200, 35] } });
map.addLayer({ id: 'cluster-count', type: 'symbol', source: 'pts', filter: ['has', 'point_count'],
layout: { 'text-field': '{point_count_abbreviated}', 'text-font': ['Noto Sans Regular'], 'text-size': 12 } });
map.addLayer({ id: 'single', type: 'circle', source: 'pts', filter: ['!', ['has', 'point_count']],
paint: { 'circle-color': '#11b4da', 'circle-radius': 6, 'circle-stroke-width': 1, 'circle-stroke-color': '#fff' } });
map.on('click', 'clusters', (e) => {
const cid = e.features[0].properties.cluster_id;
map.getSource('pts').getClusterExpansionZoom(cid, (err, z) => { if (!err) map.easeTo({ center: e.features[0].geometry.coordinates, zoom: z }); });
});
}); <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.css" />
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.mjs';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [15.5, 49.8],
zoom: 6
});
const pts = Array.from({ length: 300 }, () => ({
type: 'Feature',
geometry: { type: 'Point', coordinates: [12 + Math.random() * 7, 48.5 + Math.random() * 2.5] }
}));
map.on('load', () => {
map.addSource('pts', { type: 'geojson', data: { type: 'FeatureCollection', features: pts }, cluster: true, clusterMaxZoom: 14, clusterRadius: 50 });
map.addLayer({ id: 'clusters', type: 'circle', source: 'pts', filter: ['has', 'point_count'],
paint: { 'circle-color': ['step', ['get', 'point_count'], '#51bbd6', 50, '#f1f075', 200, '#f28cb1'], 'circle-radius': ['step', ['get', 'point_count'], 18, 50, 25, 200, 35] } });
map.addLayer({ id: 'cluster-count', type: 'symbol', source: 'pts', filter: ['has', 'point_count'],
layout: { 'text-field': '{point_count_abbreviated}', 'text-font': ['Noto Sans Regular'], 'text-size': 12 } });
map.addLayer({ id: 'single', type: 'circle', source: 'pts', filter: ['!', ['has', 'point_count']],
paint: { 'circle-color': '#11b4da', 'circle-radius': 6, 'circle-stroke-width': 1, 'circle-stroke-color': '#fff' } });
map.on('click', 'clusters', (e) => {
const cid = e.features[0].properties.cluster_id;
map.getSource('pts').getClusterExpansionZoom(cid, (err, z) => { if (!err) map.easeTo({ center: e.features[0].geometry.coordinates, zoom: z }); });
});
});
</script>
</body>
</html> import maplibregl from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [15.5, 49.8],
zoom: 6
});
const pts = Array.from({ length: 300 }, () => ({
type: 'Feature',
geometry: { type: 'Point', coordinates: [12 + Math.random() * 7, 48.5 + Math.random() * 2.5] }
}));
map.on('load', () => {
map.addSource('pts', { type: 'geojson', data: { type: 'FeatureCollection', features: pts }, cluster: true, clusterMaxZoom: 14, clusterRadius: 50 });
map.addLayer({ id: 'clusters', type: 'circle', source: 'pts', filter: ['has', 'point_count'],
paint: { 'circle-color': ['step', ['get', 'point_count'], '#51bbd6', 50, '#f1f075', 200, '#f28cb1'], 'circle-radius': ['step', ['get', 'point_count'], 18, 50, 25, 200, 35] } });
map.addLayer({ id: 'cluster-count', type: 'symbol', source: 'pts', filter: ['has', 'point_count'],
layout: { 'text-field': '{point_count_abbreviated}', 'text-font': ['Noto Sans Regular'], 'text-size': 12 } });
map.addLayer({ id: 'single', type: 'circle', source: 'pts', filter: ['!', ['has', 'point_count']],
paint: { 'circle-color': '#11b4da', 'circle-radius': 6, 'circle-stroke-width': 1, 'circle-stroke-color': '#fff' } });
map.on('click', 'clusters', (e) => {
const cid = e.features[0].properties.cluster_id;
map.getSource('pts').getClusterExpansionZoom(cid, (err, z) => { if (!err) map.easeTo({ center: e.features[0].geometry.coordinates, zoom: z }); });
});
});