Skip to content

React コンポーネント API

JapanMapSelectorReactコンポーネントのAPIリファレンスです。

インポート

javascript
import { JapanMapSelectorReact } from 'japan-map-selector/react';

Props

必須Props

プロパティ説明
prefectureDataUrlstring都道府県データのJSONファイルURL
municipalityDataUrlstring市区町村データのJSONファイルURL

オプションProps

プロパティデフォルト説明
widthnumber800地図の幅(ピクセル)
heightnumber600地図の高さ(ピクセル)
themestring | ColorTheme'default'カラーテーマ
onPrefectureSelect(prefecture: Prefecture) => void-都道府県選択時のコールバック
onMunicipalitySelect(municipality: Municipality) => void-市区町村選択時のコールバック
selectedPrefectureCodestring-初期選択都道府県コード
selectedMunicipalityCodestring-初期選択市区町村コード
selectablePrefecturesstring[]-選択可能な都道府県コードの配列
disabledPrefectureFillstring'#cccccc'選択不可都道府県の塗りつぶし色
disabledPrefectureStrokestring'#999999'選択不可都道府県の枠線色
simplificationLevel'original' | 'high' | 'medium' | 'low' | 'ultra-low''original'ポリゴンの簡略化レベル
showAttributionbooleantrue出典表示の有無
attributionOptionsAttributionOptions-出典表示のオプション

カラーカスタマイズProps

プロパティ説明
prefectureColorstring都道府県の塗りつぶし色
prefectureHoverColorstring都道府県のホバー色
municipalityColorstring市区町村の塗りつぶし色
municipalityHoverColorstring市区町村のホバー色

メソッド(ref経由)

コンポーネントのrefを通じて以下のメソッドにアクセスできます:

jsx
const mapRef = useRef();

// 使用例
mapRef.current.resetView();

利用可能なメソッド

メソッド説明パラメータ
resetView()日本全体ビューに戻るなし
selectPrefecture(code)都道府県を選択code: string
selectMunicipality(code)市区町村を選択code: string
setTheme(theme)テーマを変更theme: string | ColorTheme
setDeformMode(mode, gridSize)ディフォルメモードを設定mode: 'none' | 'grid' | 'hexagon', gridSize?: number
getPrefectures()都道府県一覧を取得なし
getSelectedMunicipalities()選択中の都道府県の市区町村一覧を取得なし

型定義

Prefecture

typescript
interface Prefecture {
  code: string;        // 都道府県コード
  name: string;        // 都道府県名
  bounds: [[number, number], [number, number]]; // 境界ボックス
  feature: GeoJSONFeature;
}

Municipality

typescript
interface Municipality {
  code: string;         // 市区町村コード
  name: string;         // 市区町村名
  prefectureCode: string; // 都道府県コード
  feature: GeoJSONFeature;
}

ColorTheme

typescript
interface ColorTheme {
  name?: string;
  prefectureFill: string;
  prefectureStroke: string;
  prefectureHoverFill: string;
  prefectureSelectedFill: string;
  municipalityFill: string;
  municipalityStroke: string;
  municipalityHoverFill: string;
  municipalitySelectedFill: string;
  backgroundColor: string;
  strokeWidth: number;
}

AttributionOptions

typescript
interface AttributionOptions {
  showLink?: boolean;    // リンクを表示するか
  position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
  style?: React.CSSProperties;
}

使用例

基本的な使用

jsx
import { JapanMapSelectorReact } from 'japan-map-selector/react';

function App() {
  const handlePrefectureSelect = (prefecture) => {
    console.log('選択された都道府県:', prefecture.name);
  };

  const handleMunicipalitySelect = (municipality) => {
    console.log('選択された市区町村:', municipality.name);
  };

  return (
    <JapanMapSelectorReact
      width={800}
      height={600}
      theme="colorful"
      prefectureDataUrl="/data/prefectures.json"
      municipalityDataUrl="/data/municipalities.json"
      onPrefectureSelect={handlePrefectureSelect}
      onMunicipalitySelect={handleMunicipalitySelect}
    />
  );
}

refを使った制御

jsx
import { useRef } from 'react';
import { JapanMapSelectorReact } from 'japan-map-selector/react';

function MapWithControls() {
  const mapRef = useRef();

  const handleReset = () => {
    mapRef.current?.resetView();
  };

  const selectTokyo = () => {
    mapRef.current?.selectPrefecture('13');
  };

  return (
    <>
      <div>
        <button onClick={handleReset}>リセット</button>
        <button onClick={selectTokyo}>東京都を選択</button>
      </div>
      <JapanMapSelectorReact
        ref={mapRef}
        prefectureDataUrl="/data/prefectures.json"
        municipalityDataUrl="/data/municipalities.json"
      />
    </>
  );
}

カスタムテーマ

jsx
const customTheme = {
  name: 'ocean',
  prefectureFill: '#0077be',
  prefectureStroke: '#005a9e',
  prefectureHoverFill: '#0099cc',
  prefectureSelectedFill: '#00bfff',
  municipalityFill: '#87ceeb',
  municipalityStroke: '#4682b4',
  municipalityHoverFill: '#add8e6',
  municipalitySelectedFill: '#b0e0e6',
  backgroundColor: '#f0f8ff',
  strokeWidth: 1.5
};

<JapanMapSelectorReact
  theme={customTheme}
  prefectureDataUrl="/data/prefectures.json"
  municipalityDataUrl="/data/municipalities.json"
/>

MIT License