住所から緯度経度を取得するスプレッドの関数
システム投入データ等で、住所から緯度経度を求めたかったのでユーザー関数を作成
使い方
以下のコードをスクリプトにコピペ
// フィールド変数
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;
}
注意
- APIキー単位で1日当たり呼び出しの上限回数がある
- 住所1件あたり緯度、経度で計2回呼び出す