緯度と経度のvarを画面に表示したいのですが、関数が最後に実行されていると思います。初期値に固執しています。目的は、ブラウザが返すジオロケーションの正確な値を画面に印刷することです。ありがとう !!!
<!DOCTYPE html>
<head>
<script>
var longitude = "10";
var latitude = "20";
</script>
</head>
<html>
<body onload="getLocation()">
<script>
var longitude = "30";
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
alert('aaab:' +longitude);
});
}else {
alert("Geolocation API is not supported in your browser.");
}
}
</script>
<script>
alert("ccc");
document.write (longitude);
document.write (latitude);
</script>
</body>
</html>
関数内で機能していることは知っていますが、これらの変数を外部で使用する方法はありますか?どこからでも呼び出すことができる1つのグローバル変数にそれらを格納できるようにしたいだけです。ありがとう