0
0

More than 3 years have passed since last update.

位置情報の取得

Last updated at Posted at 2021-04-21
index.js
<p id="loc" class="position"></p>  緯度経<span id="accuracy" class="position"></span> mです。</p> 精度
</section>
index.js
function success(pos) {
    const lat = pos.coords.latitude;
    const lng = pos.coords.longitude;
    const accuracy = pos.coords.accuracy;

    $('#loc').text(`緯度:${lat} 経度:${lng}`);
    $('#accuracy').text(accuracy);
}

function fail(error) {
    alert('位置情報の取得に失敗しました。エラーコード:' + error.code);
}

navigator.geolocation.getCurrentPosition(success, fail);

定数lat,lng,accuracyに代入していきます。
この処理内容をHTMLに出力するために、テンプレート文字列を使って整形します。
id属性loc。textメソッドの()内に含まれる出力するテキストを入れます。
精度も出力するので、同じようにid属性accuracyを処理していきます。

確かな力が身につくJavaScript「超」入門 第2版

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0