LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Wednesday, January 9, 2019

現在地の緯度, 経度から住所を取得したい


別の方法もあるかと思いますが今回やった方法を書き残しておきます
  1. getCurrentPosition()で現在地の緯度, 経度を取得する[JavaScript]
  2. reverseGeocode()で緯度, 経度から住所を返す[Google Apps Script]



コード.gs
function doGet(e) {
  return HtmlService.createHtmlOutputFromFile("index");
}

function getLocation(latitude, longitude) {
  var geocode = Maps.newGeocoder()
             .setLanguage("ja")
             .reverseGeocode(latitude, longitude);
  console.log(JSON.stringify(geocode));//geocodeの中身をログに出して見てみる
  return geocode;
}



index.html
<!DOCTYPE html>
<html>
<body>
  <button id="bt">get adress</button>
  <script>
    document.getElementById('bt').onclick = getAdress;
  
    function getAdress() {
      navigator.geolocation.getCurrentPosition(success);
    }

    function success(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      toGetLocation(latitude, longitude);
    }

    function toGetLocation(latitude, longitude) {
      google.script.run
        .withFailureHandler(onFailure)
        .withSuccessHandler(onSuccess)
        .getLocation(latitude, longitude);
    }

    function onSuccess(geocode) {
      alert(geocode['results'][0]['formatted_address']);//geocodeの中から住所を取得する
    }

    function onFailure(e) {
      alert([e.message, e.stack]);
    }
  </script>
</body>
</html>


get adressボタンをクリックすると現在地の住所がアラートに表示されます


Latest post

スプレッドシートA列にある複数のテキストをスライドに追加したい(Google Apps Script)

今回Google Apps Scriptでやりたいこと GoogleスプレッドシートA列にある複数の値を取得して Googleスライドに渡して 図形オブジェクトのテキストとして追加したい ① スプレッドシートのA列に値を入れておく ② Code.gsのinsertNewShape...