Apps Scriptリファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2019年1月9日水曜日

現在地の緯度, 経度を取得する(Geolocation API)


Geolocation APIで現在地の緯度, 経度を取得してアラートに表示してみる





コード.gs
function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}
意訳
この機能がやること
指定したHTMLファイルを表示する




index.html
<!DOCTYPE html>
<html>
<body>
<button id="bt">get</button>
  <script>
    document.getElementById('bt').onclick = currentPosition

    function currentPosition() {
      navigator.geolocation.getCurrentPosition(success);
    }

    function success(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      alert([latitude, longitude]);
    }
  </script>
</body>
</html>
意訳
  


getボタンを置く

getボタンをクリックしたらcurrentPositionを実行する

この機能がやること
現在地を取得してsuccessに渡す


この機能がやること
渡された現在地の緯度を取得して
渡された現在地の経度を取得して
アラートに表示する






getボタンをクリックすると現在地の緯度, 経度がアラートに表示されます


補足

参考にしたGeolocation APIのページにも記載されていましたが、HTTPSで利用できるようです



geolocation-api.html:797 [Deprecation] getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.


参考

Geolocation API
https://developer.mozilla.org/ja/docs/Web/API/Geolocation/Using_geolocation
Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Geolocation.getCurrentPosition()


Latest post

Googleドキュメントに見出しを追加したい

今回の例では、ドキュメントの末尾に「見出しD」 を追加します。 見出しA, B, C, Dのスタイルは、見出し3 ( HEADING3 ) に設定しています。  下記Code.gsの  GOOGLE_DOCUMENT_URL を設定して  addHeadingToEnd()  を...