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
Geolocation.getCurrentPosition()
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()