diff --git a/1218kadai.js b/1218kadai.js new file mode 100644 index 0000000..3f82e41 --- /dev/null +++ b/1218kadai.js @@ -0,0 +1,64 @@ +document.addEventListener("DOMContentLoaded", function () { + var mymap = L.map("locationmap").setView([38.891, 139.824], 16); + L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', { + attribution: '© OpenStreetMap contributors' + }).addTo(mymap); + + var markers = L.layerGroup().addTo(mymap); + + var watchId = null; + var targetLocation = L.latLng(38.891, 139.824); + var proximityThreshold = 100; + + var statusElement = document.getElementById("status"); + + function stopLogging() { + if (watchId !== null) { + navigator.geolocation.clearWatch(watchId); + watchId = null; + } + statusElement.textContent = " - 停止中"; + console.log("ロギング停止"); + } + + + function startLogging() { + stopLogging(); + watchId = navigator.geolocation.watchPosition(onSuccess, onError, { + enableHighAccuracy: true, + timeout: 5000, + maximumAge: 0 + }); + statusElement.textContent = " - ロギング中"; + console.log("ロギング開始"); + } + + + function onSuccess(position) { + var lat = position.coords.latitude; + var lng = position.coords.longitude; + var currentLocation = L.latLng(lat, lng); + + + var distance = currentLocation.distanceTo(targetLocation); + + + var popupMessage = + distance <= proximityThreshold + ? "目標地点まで" + Math.round(distance) + "メートル!近づきました!" + : "現在地: lat=" + lat + ", lng=" + lng + "
目標地点まで" + Math.round(distance) + "メートル"; + + markers.addLayer(L.marker(currentLocation).bindPopup(popupMessage).openPopup()); + mymap.panTo(currentLocation); + + console.log("現在地: 緯度 " + lat + ", 経度 " + lng + ", 距離: " + distance + "メートル"); + } + + function onError(error) { + console.error("位置情報取得失敗", error); + } + + + document.getElementById("start").addEventListener("click", startLogging); + document.getElementById("stop").addEventListener("click", stopLogging); +});