<!DOCTYPE html>
<html>
<body>
<h2>JavaScript 地理位置情報API</h2>
<p>ボタンをクリックして座標を取得します。</p>
<button onclick="getLocation()">試す</button>
<p id="demo"></p>
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}