0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[GAS] Maps.Geocoderで住所や施設名から郵便番号を検索・取得できる

Last updated at Posted at 2021-02-23

はじめに

約1000件近くの住所リストから郵便番号を調べる必要があったため、色々調べた際の備忘録です。

郵便番号と住所を紐付けたCSVデータ1日本郵政のサイトからダウンロードできます。しかし、それを使ってシステムを構築するとなれば、およそ12万件のデータをクレンジングしなければなりません。

もっとお手軽に構築できないかと調べたところ、Maps.Geocoderで住所や施設名から郵便番号を検索・取得できるということで、Google Apps Script(以降GAS)を使用するという結論2 に至りました。

Maps.Geocoder

主な利用用途としては住所から緯度経度取得(ジオコーディング)、及びその逆(リバースジオコーディング)かと思います。

レスポンスのデータ構造は、Mapsクラスのドキュメントではなく、Google Geocoding APIのドキュメントにて確認できます。

レスポンスのデータ構造サンプル
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ] // ←郵便番号データ
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         // 省略
      }
   ],
   "status" : "OK"
}

この内、住所に関しては国名や〒を含めた文字列3 であるformatted_address、都道府県や市区町村といったレベル別に分割されたaddress_componentsが取得可能で、いずれにおいても郵便番号を取得できます。

住所もしくは施設名から郵便番号を返す

引数に住所もしくは施設名を取り、formatted_addressより郵便番号を抽出するかaddress_componentsより取得します。

Sample.gs
//formatted_addressより郵便番号を抽出
const hoge = addr => {
  const res = Maps.newGeocoder().setLanguage('ja').geocode(addr)
  return res.results[0].formatted_address.slice(4,12)
}

//address_componentsより取得
const fuga = addr => {
  const res = Maps.newGeocoder().setLanguage('ja').geocode(addr)
  const addr = res.results[0].address_components
  return addr[addr.length-1].short_name // long_nameでも結果は同じ
}

Logger.log(hoge('東京駅'))                   // 100-0005
Logger.log(fuga('東京都千代田区丸の内1丁目')) // 100-0005

最後に

郵便番号をハイフン抜きにするにはreplace('-','')でいいと思います。
エラー判定はres.status、複数候補の有無はres.results.lengthで確認できますので、その後の処理は仕様に合わせてカスタマイズすれば良いと思います。

[追記]

タイトルのGeocodeをGeocoderに修正しました。
修正ついでの補足情報ですが、無料アカウントの場合、1日のAPIアクセス制限は1000件となっています。

  1. 各都道府県別のデータと都道府県の全データがダウンロード可能。

  2. 結論と言いつつも、約1000件の一括走査は実行時制限に引っかかるため、最適解ではないです。

  3. hoge('東京駅')では、日本、〒100-0005 東京都千代田区丸の内1丁目

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?