LoginSignup
49

More than 5 years have passed since last update.

【GAS】住所から緯度経度を取得する

Last updated at Posted at 2015-06-15

たまに、大量な住所情報から緯度経度が欲しかったりしますよね…
え、しない?

Google Geocoding APIを使用して、JSONを取得、表示しているだけです。
本来ならXMLHttpRequestを使うところですが、GASだとUrlFetchAppで簡単に取得できます。

スプレッドシートの関数にすると、いっぺんに結果が返ってくるので少し気持ちいいです。

コード.gs
function getLocation(address) {
  var requestUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address; // Google Geocoding API
  var result = JSON.parse( UrlFetchApp.fetch(requestUrl) );
  if ( result['status'] === 'ZERO_RESULTS' ) { return 'No Result.' }
  var location = result['results'][0]['geometry']['location'];
  return [ [ location['lat'] , location['lng'] ] ];
}

実は、住所じゃなくてもランドマーク名を引数に渡せば緯度経度が返ってきます。

サンプル

スプレッドシート
ソースコード

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
49