コード例
import { useMemo } 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';
import '@mapconductor/react-for-maplibre/style.css';
export default function App() {
// (1) 地図の作成
const initialCamera = createMapCameraPosition({
position: createGeoPoint({ latitude: 35.6812, longitude: 139.7671 }),
zoom: 12,
});
const mapViewState = useMapLibreViewState({
mapDesignType: MapLibreDesign.OsmBrightJa,
cameraPosition: initialCamera,
});
// (2) データと操作用Stateの準備
// Inline points work without the example app's public data files.
const heatmapPoints = useMemo(() => Array.from({ length: 100 }, (_, index) =>
new HeatmapPointState({
id: `point-${index}`,
position: createGeoPoint({
latitude: 35.68 + Math.sin(index * 2.4) * (index % 10) * 0.001,
longitude: 139.77 + Math.cos(index * 2.4) * (index % 10) * 0.001,
}),
}),
), []);
// (3) 地図とオーバーレイの描画
return (
<div style={{ height: 500, position: 'relative', display: 'flex', flexDirection: 'column' }}>
<MapLibreMapView2D state={mapViewState}>
<HeatmapOverlay>
<HeatmapPoints states={heatmapPoints} />
</HeatmapOverlay>
</MapLibreMapView2D>
</div>
);
}
コードの読み方
HeatmapOverlay は地理座標の点群を密度マップとして描画します。
この短い例は外部ファイルを使わず、useMemo で説明用の HeatmapPointState を100件生成します。HeatmapPoints が点群をまとめてオーバーレイへ渡します。