コード例
import { useMemo, useState } from 'react';
import { createGeoPoint, createMapCameraPosition, createMarkerState } from '@mapconductor/js-sdk-core';
import { InfoBubble, Marker } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
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の準備
const position = useMemo(() => createGeoPoint({ latitude: 35.6812, longitude: 139.7671 }), []);
const [selected, setSelected] = useState(false);
const marker = useMemo(() => createMarkerState({
id: 'place', position, onClick: () => setSelected(true),
}), [position]);
// (3) 地図とオーバーレイの描画
return (
<div style={{ height: 500, position: 'relative', display: 'flex', flexDirection: 'column' }}>
<MapLibreMapView2D state={mapViewState}>
<Marker state={marker} />
{selected && (
<InfoBubble marker={marker}>Simple text content</InfoBubble>
)}
</MapLibreMapView2D>
</div>
);
}
コードの読み方
ユーザーが選択したマーカーに、シンプルな React コンテンツの吹き出しを固定します。
真偽値の selected を useState で保持し、マーカーの onClick がそれを true にします。
InfoBubble は selected が true の間だけ描画され、プレーンテキストを表示することで、マーカーに固定した吹き出しの最小構成を示します。