LoginSignup
0
2

More than 5 years have passed since last update.

住所から緯度経度を取得するスプレッドの関数

Posted at

住所から緯度経度を取得するスプレッドの関数

システム投入データ等で、住所から緯度経度を求めたかったのでユーザー関数を作成

使い方

image.png
関数を赤色のセルに貼り付けてね

以下のコードをスクリプトにコピペ

// フィールド変数
var apiKey = "xxxx"; //Google Cloud Platform >認証情報 >APIキー を入れる

//緯度
function getLat(address) {
  var location = getLatLng(address);
  return location['lat'];
}

//経度
function getLng(address) {
  var location = getLatLng(address);

  return location['lng'];
}

//APIを呼び出して住所から緯度経度を求める
function getLatLng(address) {
  if (address === '') {
      return
    }

    var api_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURI(address);
    api_url += '&key='+apiKey;
    var response = UrlFetchApp.fetch(api_url);

    var result = JSON.parse(response);
    var location = result['results'][0]

    if (typeof location === 'undefined') {
      return
    }
    location = location['geometry']['location'];
    return location;
}

注意

  1. APIキー単位で1日当たり呼び出しの上限回数がある
  2. 住所1件あたり緯度、経度で計2回呼び出す
0
2
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
2