<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>現在地取得</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<style>
#map {
height: 400px;
}
.big-button {
font-size: 16px;
padding: 10px 20px;
}
</style>
</head>
<body>
<h1>現在地取得と表示</h1>
<button onclick="getCurrentLocation()" class="big-button">現在地取得</button>
<button onclick="copyLocation()" class="big-button">位置情報コピー</button>
<p id="location"></p>
<div id="map"></div>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script>
var map = L.map('map').setView([0, 0], 18);
L.tileLayer('https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png', {
attribution: '<a href="https://maps.gsi.go.jp/development/ichiran.html">国土地理院</a>'
}).addTo(map);
var marker;
function getCurrentLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById("location").innerHTML = latitude + "," + longitude;
if (marker) {
marker.setLatLng([latitude, longitude]).update();
} else {
marker = L.marker([latitude, longitude]).addTo(map);
}
map.setView([latitude, longitude], 18);
}
function copyLocation() {
var locationText = document.getElementById("location").innerText;
navigator.clipboard.writeText(locationText).then(function() {
alert("位置情報がコピーされました!");
}).catch(function(err) {
console.error('位置情報のコピーに失敗しました:', err);
});
}
</script>
</body>
</html>