1
1

More than 1 year has passed since last update.

住所の一覧から緯度・経度を取得する

Posted at

はじめに

膨大な住所の一覧をもとに緯度と経度を取得したいということで「Googleマップで住所検索⇒検索されたポイントを右クリック⇒緯度・経度取得」でも取得はできるのですが、手間がかかりすぎるため一括で取得するためにGeocoding APIを利用してみました。

サンプルコード

Geo2LatLon.html
<html>
<head>
  <title>Geo2LatLon</title>
  <!-- referrence by https://github.com/madefor/postal-code-api -->
  <style>
    textarea{width:100%;}
    input{margin-top:5px;padding:5px 20px 5px 20px;font-weight:bold;font-size:20px;}
  </style>
  <script src="https://cdn.geolonia.com/community-geocoder.js"></script>
</head>
<body>
  <h2>Geo2LatLon</h2>
  <h3>概要:住所から緯度経度を取得するツールです。</h3>
  <h3>使い方</h3>
  <ol>
    <li>テキストエリアに住所を入力します。複数の場合は改行区切りで入力してください。</li>
    <li>「実行」ボタンを押下します。</li>
    <li>csv形式でファイルがダウンロードされます。</li>
  </ol>
  <textarea id="geoList" rows="20"></textarea>
  <input id='exec' type='button' value='実行' />
  <script>
    document.getElementById('exec').addEventListener('click', () => {
      const geoList = document.getElementById('geoList');
      if (geoList.value) {
        const lines = geoList.value.split('\n');
        const promises = [];
        for (let i = 0; i < lines.length; i++) {
          const addr = lines[i];
          if (addr) {
            const func = new Promise((resolve, reject) => {
              getLatLng(addr, (geo) => {
                const data = [addr, geo.lat, geo.lng].join(',');
                resolve(data);
              });
            });
            promises.push(func);
          }
        }

        Promise.all(promises).then((results) => {
          results.unshift(['住所', '緯度', '経度'].join(','))
          const data = results.join('\n');
          const filename = 'geo2latlon.csv';
          const bom = new Uint8Array([0xef, 0xbb, 0xbf]);
          const blob = new Blob([bom, data], {type: 'text/csv'});
          const url = (window.URL || window.webkitURL);
          const download = document.createElement('a');
          download.href = url.createObjectURL(blob);
          download.download = filename;
          download.click();
          url.revokeObjectURL(url);
        });
      }
    });
  </script>
</body>
</html>

ただ精度はあまり良くないようなので(Google Maps APIも同様)、Google Mapsの右クリックで取得するのが一番良さそうです。。。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1