LANG SELRCT

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

Wednesday, January 9, 2019

現在地の緯度, 経度を取得する(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

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

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