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を処理していきます。