Code example
import { useMemo, useState } from 'react';
import {
MarkerTilingOptions,
createGeoPoint,
createMapCameraPosition,
createMarkerState,
type MarkerState,
} from '@mapconductor/js-sdk-core';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
import { MarkerClusterGroup } from '@mapconductor/react-marker-clustering';
// (1) Create the map
const initialCamera = createMapCameraPosition({
position: createGeoPoint({ latitude: 35.6812, longitude: 139.7671 }),
zoom: 12,
});
const mapViewState = useMapLibreViewState({
mapDesignType: MapLibreDesign.OsmBrightJa,
cameraPosition: initialCamera,
});
// (2) Prepare the data and interaction state
const [selected, setSelected] = useState<MarkerState | null>(null);
const postOfficeMarkers = useMemo(() => postOffices.map(office => createMarkerState({
id: office.id,
position: createGeoPoint({ latitude: office.lat, longitude: office.lng }),
extra: office,
onClick: markerState => setSelected(markerState),
})), [postOffices]);
const markerTilingOptions = {
...MarkerTilingOptions.Default,
iconScaleCallback: (_state: MarkerState, zoom: number) =>
zoom > 10 ? 0.8 : zoom > 5 ? 0.5 : 0.2,
};
// (3) Render the map and its overlays
<MapLibreMapView2D state={mapViewState}>
<MarkerClusterGroup
markers={postOfficeMarkers}
clusterIconProvider={clusterIconProvider}
onClusterClick={zoomToCluster}
minClusterSize={3}
clusterRadiusPx={80}
/>
</MapLibreMapView2D>
How the code works
Cluster a large marker collection and provide a custom cluster icon and click behavior through the extension API.
The same postOfficeMarkers array feeds MarkerClusterGroup instead of <Markers>, so nearby offices are grouped automatically as the map zooms out.
clusterIconProvider draws the cluster badge, onClusterClick runs zoomToCluster to open a group, and minClusterSize with clusterRadiusPx tune how aggressively points merge.