Code example
import { useEffect, useState } from 'react';
import { createGeoPoint, createMapCameraPosition } from '@mapconductor/js-sdk-core';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
import { HeatmapOverlay, HeatmapPointState, HeatmapPoints } from '@mapconductor/react-heatmap';
// (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 [heatmapPoints, setHeatmapPoints] = useState<HeatmapPointState[]>([]);
useEffect(() => {
fetch('/postoffice/postoffices.json')
.then(response => response.json() as Promise<[number, number][]>)
.then(data => setHeatmapPoints(data.map(([latitude, longitude], index) =>
new HeatmapPointState({
id: `post-office-${index}`,
position: createGeoPoint({ latitude, longitude }),
}),
)));
}, []);
// (3) Render the map and its overlays
<MapLibreMapView2D state={mapViewState}>
<HeatmapOverlay>
<HeatmapPoints states={heatmapPoints} />
</HeatmapOverlay>
</MapLibreMapView2D>
How the code works
Compose weighted geographic points inside the heatmap extension overlay.
A useEffect fetches postoffices.json and maps each coordinate into a HeatmapPointState kept in React state.
The <HeatmapPoints> inside <HeatmapOverlay> composes the whole set at once, and the extension renders the density map on any provider.