MapConductor React SDK Samples

測地線ポリゴン | MapLibre 3D | MapConductor React SDK

MapConductor React SDKでMapLibre 3Dを使用する測地線ポリゴンのサンプルです。

このインタラクティブサンプルでは、MapConductorの抽象APIを通じてMapLibre 3D上で測地線ポリゴンを実装します。

コード例

import { useMemo } from 'react';
import { createGeoPoint, createMapCameraPosition, createPolygonState } from '@mapconductor/js-sdk-core';
import { Polygon } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
import '@mapconductor/react-for-maplibre/style.css';


export default function App() {
  // (1) 地図の作成


  const initialCamera = createMapCameraPosition({
    position: createGeoPoint({ latitude: 40, longitude: -160 }),
    zoom: 2,
  });


  const mapViewState = useMapLibreViewState({
    mapDesignType: MapLibreDesign.OsmBrightJa,
    cameraPosition: initialCamera,
  });


  // (2) データと操作用Stateの準備
  const longDistancePoints = useMemo(() => [
    createGeoPoint({ latitude: 35.68, longitude: 139.77 }),
    createGeoPoint({ latitude: 37.77, longitude: -122.42 }),
    createGeoPoint({ latitude: 21.31, longitude: -157.86 }),
  ], []);
  const geodesicPolygon = useMemo(() => createPolygonState({
    id: 'geodesic', points: longDistancePoints, geodesic: true,
  }), [longDistancePoints]);
  const straightPolygon = useMemo(() => geodesicPolygon.copy({
    id: 'straight', geodesic: false, strokeColor: '#2563eb', fillColor: 'rgba(37, 99, 235, 0.15)',
  }), [geodesicPolygon]);
  const polygons = [geodesicPolygon, straightPolygon];


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


  return (
    <div style={{ height: 500, position: 'relative', display: 'flex', flexDirection: 'column' }}>
      <MapLibreMapView state={mapViewState}>
        {polygons.map(polygon => (
          <Polygon key={polygon.id} state={polygon} />
        ))}
      </MapLibreMapView>
    </div>
  );
}

コードの読み方

長距離や日付変更線をまたぐ形状で、測地線と非測地線のポリゴン辺を比較します。

geodesicPolygon は geodesic:true で作成し、straightPolygon は同じ longDistancePoints に対して geodesic:false を指定した .copy です。

両方を描画すると、測地線の辺が最短経路に沿って湾曲し、非測地線の辺は投影上でまっすぐ描かれる違いが分かります。