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 Formsで記述式の質問に字数制限を設定したい

記述式の質問には「回答の検証」を設定することができます フォームの編集画面 右下の︙メニューで「回答の検証」を選択します 検証方法には「数値」「テキスト」「長さ」「正規表現」という種類があります 今回は字数制限したいので「長さ」を選びます 長さには「最大文字数」か「最小文字数」を...