Something went wrong. Try again.
source code of sayemon10.com sayemon10.com
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287<script lang="ts" generics="P extends GeoJSON.GeoJsonProperties"> import { getContext } from 'svelte'; import * as MapLibreGL from 'maplibre-gl';
interface Props { /** GeoJSON FeatureCollection data or URL to fetch GeoJSON from */ data: string | GeoJSON.FeatureCollection<GeoJSON.Point, P>; /** Maximum zoom level to cluster points on (default: 14) */ clusterMaxZoom?: number; /** Radius of each cluster when clustering points in pixels (default: 50) */ clusterRadius?: number; /** Colors for cluster circles: [small, medium, large] based on point count (default: ["#22c55e", "#eab308", "#ef4444"]) */ clusterColors?: [string, string, string]; /** Point count thresholds for color/size steps: [medium, large] (default: [100, 750]) */ clusterThresholds?: [number, number]; /** Color for unclustered individual points (default: "#3b82f6") */ pointColor?: string; /** Callback when an unclustered point is clicked */ onpointclick?: ( feature: GeoJSON.Feature<GeoJSON.Point, P>, coordinates: [number, number] ) => void; /** Callback when a cluster is clicked. If not provided, zooms into the cluster */ onclusterclick?: (clusterId: number, coordinates: [number, number], pointCount: number) => void; }
let { data, clusterMaxZoom = 14, clusterRadius = 50, clusterColors = ['#22c55e', '#eab308', '#ef4444'], clusterThresholds = [100, 750], pointColor = '#3b82f6', onpointclick, onclusterclick }: Props = $props();
const mapCtx = getContext<{ getMap: () => MapLibreGL.Map | null; isStyleReady: () => boolean; }>('map');
const id = crypto.randomUUID(); const sourceId = $derived(`cluster-source-${id}`); const clusterLayerId = $derived(`clusters-${id}`); const clusterCountLayerId = $derived(`cluster-count-${id}`); const unclusteredLayerId = $derived(`unclustered-point-${id}`);
// Add source and layers when map is ready $effect(() => { const map = mapCtx.getMap(); const loaded = mapCtx.isStyleReady();
if (!loaded || !map) return;
// Remove existing layers and source if they exist try { if (map.getLayer(clusterCountLayerId)) map.removeLayer(clusterCountLayerId); if (map.getLayer(unclusteredLayerId)) map.removeLayer(unclusteredLayerId); if (map.getLayer(clusterLayerId)) map.removeLayer(clusterLayerId); if (map.getSource(sourceId)) map.removeSource(sourceId); } catch { // ignore }
// Add clustered GeoJSON source map.addSource(sourceId, { type: 'geojson', data, cluster: true, clusterMaxZoom, clusterRadius });
// Add cluster circles layer map.addLayer({ id: clusterLayerId, type: 'circle', source: sourceId, filter: ['has', 'point_count'], paint: { 'circle-color': [ 'step', ['get', 'point_count'], clusterColors[0], clusterThresholds[0], clusterColors[1], clusterThresholds[1], clusterColors[2] ], 'circle-radius': [ 'step', ['get', 'point_count'], 20, clusterThresholds[0], 30, clusterThresholds[1], 40 ], 'circle-stroke-width': 1, 'circle-stroke-color': '#fff', 'circle-opacity': 0.85 } });
// Add cluster count text layer map.addLayer({ id: clusterCountLayerId, type: 'symbol', source: sourceId, filter: ['has', 'point_count'], layout: { 'text-field': '{point_count_abbreviated}', 'text-font': ['Open Sans'], 'text-size': 12 }, paint: { 'text-color': '#fff' } });
// Add unclustered point layer map.addLayer({ id: unclusteredLayerId, type: 'circle', source: sourceId, filter: ['!', ['has', 'point_count']], paint: { 'circle-color': pointColor, 'circle-radius': 5, 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' } });
return () => { try { if (map.getLayer(clusterCountLayerId)) map.removeLayer(clusterCountLayerId); if (map.getLayer(unclusteredLayerId)) map.removeLayer(unclusteredLayerId); if (map.getLayer(clusterLayerId)) map.removeLayer(clusterLayerId); if (map.getSource(sourceId)) map.removeSource(sourceId); } catch { // ignore } }; });
// Update source data when data prop changes (only for non-URL data) $effect(() => { const map = mapCtx.getMap(); const loaded = mapCtx.isStyleReady();
if (!loaded || !map || typeof data === 'string') return;
const source = map.getSource(sourceId) as MapLibreGL.GeoJSONSource | undefined; if (source) { source.setData(data); } });
// Update layer styles when props change $effect(() => { const map = mapCtx.getMap(); const loaded = mapCtx.isStyleReady();
if (!loaded || !map) return;
// Update cluster layer colors and sizes if (map.getLayer(clusterLayerId)) { map.setPaintProperty(clusterLayerId, 'circle-color', [ 'step', ['get', 'point_count'], clusterColors[0], clusterThresholds[0], clusterColors[1], clusterThresholds[1], clusterColors[2] ]); map.setPaintProperty(clusterLayerId, 'circle-radius', [ 'step', ['get', 'point_count'], 20, clusterThresholds[0], 30, clusterThresholds[1], 40 ]); }
// Update unclustered point layer color if (map.getLayer(unclusteredLayerId)) { map.setPaintProperty(unclusteredLayerId, 'circle-color', pointColor); } });
// Handle click events $effect(() => { const map = mapCtx.getMap(); const loaded = mapCtx.isStyleReady();
if (!loaded || !map) return;
// Cluster click handler - zoom into cluster const handleClusterClick = async ( e: MapLibreGL.MapMouseEvent & { features?: MapLibreGL.MapGeoJSONFeature[]; } ) => { const features = map.queryRenderedFeatures(e.point, { layers: [clusterLayerId] }); if (!features.length) return;
const feature = features[0]; const clusterId = feature.properties?.cluster_id as number; const pointCount = feature.properties?.point_count as number; const coordinates = (feature.geometry as GeoJSON.Point).coordinates as [number, number];
if (onclusterclick) { onclusterclick(clusterId, coordinates, pointCount); } else { // Default behavior: zoom to cluster expansion zoom const source = map.getSource(sourceId) as MapLibreGL.GeoJSONSource; const zoom = await source.getClusterExpansionZoom(clusterId); map.easeTo({ center: coordinates, zoom }); } };
// Unclustered point click handler const handlePointClick = ( e: MapLibreGL.MapMouseEvent & { features?: MapLibreGL.MapGeoJSONFeature[]; } ) => { if (!onpointclick || !e.features?.length) return;
const feature = e.features[0]; const coordinates = (feature.geometry as GeoJSON.Point).coordinates.slice() as [ number, number ];
// Handle world copies while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) { coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360; }
onpointclick(feature as unknown as GeoJSON.Feature<GeoJSON.Point, P>, coordinates); };
// Cursor style handlers const handleMouseEnterCluster = () => { map.getCanvas().style.cursor = 'pointer'; }; const handleMouseLeaveCluster = () => { map.getCanvas().style.cursor = ''; }; const handleMouseEnterPoint = () => { if (onpointclick) { map.getCanvas().style.cursor = 'pointer'; } }; const handleMouseLeavePoint = () => { map.getCanvas().style.cursor = ''; };
map.on('click', clusterLayerId, handleClusterClick); map.on('click', unclusteredLayerId, handlePointClick); map.on('mouseenter', clusterLayerId, handleMouseEnterCluster); map.on('mouseleave', clusterLayerId, handleMouseLeaveCluster); map.on('mouseenter', unclusteredLayerId, handleMouseEnterPoint); map.on('mouseleave', unclusteredLayerId, handleMouseLeavePoint);
return () => { map.off('click', clusterLayerId, handleClusterClick); map.off('click', unclusteredLayerId, handlePointClick); map.off('mouseenter', clusterLayerId, handleMouseEnterCluster); map.off('mouseleave', clusterLayerId, handleMouseLeaveCluster); map.off('mouseenter', unclusteredLayerId, handleMouseEnterPoint); map.off('mouseleave', unclusteredLayerId, handleMouseLeavePoint); }; });</script>