MapConductor React SDK Samples

地表画像 | MapLibre | MapConductor React SDK

MapConductor React SDKでMapLibreを使用する地表画像のサンプルです。

このインタラクティブサンプルでは、MapConductorの抽象APIを通じてMapLibre上で地表画像を実装します。

コード例

import { useState } from 'react';
import {
  createGeoPoint,
  createGeoRectBounds,
  createGroundImageState,
  createMapCameraPosition,
  createMarkerState,
  type MarkerState,
} from '@mapconductor/js-sdk-core';
import { GroundImage, Markers } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';


// (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 [groundImageState] = useState(() => createGroundImageState({
  id: 'historic-map', imageUrl, bounds: initialBounds, opacity: 0.7,
}));


const moveSouthWest = (markerState: MarkerState) => {
  groundImageState.bounds = createGeoRectBounds({
    southWest: markerState.position,
    northEast: groundImageState.bounds.northEast,
  });
};


const southWestMarker = createMarkerState({
  id: 'south-west', position: groundImageState.bounds.southWest!,
  draggable: true, onDrag: moveSouthWest, onDragEnd: moveSouthWest,
});
const cornerMarkers = [southWestMarker, northEastMarker];


// (3) 地図とオーバーレイの描画


<MapLibreMapView2D state={mapViewState}>
  <GroundImage state={groundImageState} />
  <Markers states={cornerMarkers} />
</MapLibreMapView2D>

コードの読み方

GroundImageState は1つのインスタンスを保持し、ドラッグできる隅のマーカーから bounds を更新します。

GroundImageState は useState で1つだけ生成して差し替えず、隅をドラッグすると moveSouthWest が新しい createGeoRectBounds を groundImageState.bounds へ代入します。

state は Observable なので、bounds の代入ごとに地図プロバイダーへ伝わり、重ねた画像が隅のマーカーに追従して伸縮します。