国土地理院APIでお手軽ジオコーディング&逆ジオコーディングの画像
芽萌丸プログラミング部 @programming
投稿日 2019/08/23
更新日 2022/06/16 ✏

GIS

国土地理院APIでお手軽ジオコーディング&逆ジオコーディング

Google Maps API を使わなくても、会員登録不要でお手軽にジオコーディング&逆ジオコーディングを行う方法があります。それは、国土地理院が公開してくれているAPIを使わせてもらうことです。

目次

ジオコーディングAPI

場所の名称から緯度経度を取得します。

例: "札幌駅"で検索:

GET https://msearch.gsi.go.jp/address-search/AddressSearch?q=%E6%9C%AD%E5%B9%8C%E9%A7%85

結果としてGeoJSONが返ってきます。

結果:

[{
  "geometry": {
    "coordinates": [141.354309, 43.063042],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "addressCode": "",
    "title": "北海道札幌市"
  }
}, {
  "geometry": {
    "coordinates": [141.3499316, 43.06682465],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "addressCode": "01101",
    "title": "中央警察署札幌駅前交番"
  }
}, {
  "geometry": {
    "coordinates": [141.3503861, 43.07004996],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "addressCode": "01102",
    "title": "北警察署札幌駅北口交番"
  }
}, {
  "geometry": {
    "coordinates": [141.351173694444, 43.0686718333333],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "addressCode": "01102",
    "title": "札幌駅"
  }
}, {
  "geometry": {
    "coordinates": [141.3519002, 43.06904808],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "addressCode": "01102",
    "title": "札幌駅パセオ郵便局"
  }
}, {
  "geometry": {
    "coordinates": [141.384829777778, 43.0517861944444],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "addressCode": "01104",
    "title": "東札幌駅"
  }
}, {
  "geometry": {
    "coordinates": [141.472467318889, 43.0385570441667],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "addressCode": "01108",
    "title": "新札幌駅"
  }
}, {
  "geometry": {
    "coordinates": [141.47208336, 43.03918904],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "addressCode": "01108",
    "title": "新札幌駅デュオ郵便局"
  }
}]

逆ジオコーディングAPI

今度は逆に緯度経度から場所の名称を取得します。

例: 北緯43.0686718333333, 東経141.351173694444 ("札幌駅") で検索:

GET https://mreversegeocoder.gsi.go.jp/reverse-geocoder/LonLatToAddress?lat=43.0686718333333&lon=141.351173694444

結果:

{
  "results": {
    "muniCd": "01102",
    "lv01Nm": "北六条西三丁目"
  }
}

結果のlv01Nmが地域名です。muniCdhttps://maps.gsi.go.jp/js/muni.jsの変換表を使って都道府県&市区町村に変換できます。例えば、上記のmuniCd "01102" の場合、GSI.MUNI_ARRAY["1102"] = '1,北海道,1102,札幌市 北区'として登録されていますので、 北海道 札幌市 北区 北六条西三丁目 だということが分かります。

NOTE

嬉しいCORS許可

上記のAPIはCORSが許可 (レスポンスヘッダにAccess-Control-Allow-Origin: *が指定) されており、ブラウザアプリケーションにはとても使い勝手の良い仕様となっています。国土地理院の中の人には感謝して使わせていただきましょう。ご利用の際はあまり負荷を掛け過ぎないようご注意を。

テスト実行

ひとこと

今回ご紹介した国土地理院のAPIは芽萌丸ツールズの地図ツールでも利用させていただいております。


芽萌丸プログラミング部
芽萌丸プログラミング部 @programming
プログラミング関連アカウント。Web標準技術を中心に書いていきます。フロントエンドからサーバサイドまで JavaScript だけで済ませたい人たちの集いです。記事は主に @TanakaSoftwareLab が担当。