document.addEventListener("DOMContentLoaded", () => { var koeki = L.map("gpsmap").setView([38.891, 139.824], 16); L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', { attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> \ contributors' }).addTo(koeki); var gpsmarker = L.marker(koeki.getCenter()).addTo(koeki); gpsmarker.bindPopup("STARTおしてね").openPopup(); var nTrial = 100 var watchId = null; // 最初はnullにしておく function stopGPS() { // watchが動いていたら止めてnullにする console.log("watchId="+watchId); if (watchId !== null) { // nullかどうかで比較しないとだめ(初期値0) navigator.geolocation.clearWatch(watchId); document.getElementById("title").textContent = "stop"; gpsmarker.setPopupContent("停めました"); } watchId = null; } function tryWatchGPS() { stopGPS(); // 二重で動かないように注意する watchId = navigator.geolocation.watchPosition( onSuccess, onError,{ maximumAge: 0, timeout: 3000, enableHighAccuracy: true}); document.getElementById("title").textContent = "START!!"; } function onSuccess(pos) { // GPS信号が取れたらここに来る var latlng = L.latLng([pos.coords.latitude, pos.coords.longitude]); mymap.panTo(latlng);// 地図の中心をそこにする console.log(latlng); // ★★★★★ ここから gpsmarker.setLatLng(latlng).setPopupContent( "ここは lat="+latlng.lat+", lng="+latlng.lng+" です." ).openPopup(); var x = latlng.lng, y = latlng.lat; if(x > 38.89121654159561&& x < 38.89489909419531 && y >139.81877088546753 && y <139.8199188709259 ){ window.location.href('https://www.koeki-u.ac.jp/'); } if(x > 38.89194304668247 && x <38.89256098768962 && y >139.82115268707275 && y < 139.82362031936646){ window.location.href('https://mokkedano.net/spot/41454'); } if(x > 38.891291697638835&& x < 38.89157561975117 && y >139.8233950138092 && y < 139.82402801513672){ window.location.href('http://www.domonken-kinenkan.jp/'); } if(x > 38.88960484286446 && x <38.88998063081193 && y > 139.82387781143188 && y <139.82454299926758 ){ window.location.href('http://www.city.sakata.lg.jp/bunka/bunkazai/rekishibunkashisetsu/nansyuujinjya.html'); } if(x > 38.898690003303436 && x < 38.89915759040677 && y > 139.8185133934021 && y < 139.81859922409058){ window.location.href('https://loco.yahoo.co.jp/place/g-cRMa7sAzQvU/'); } if(x > 38.894189319999676 && x < 38.89436467663718 && y >139.81586337089539 && y < 139.81691479682922 ){ window.location.href('http://www.city.sakata.lg.jp/shisei/shisetsu/kyoiku/annai_dewayuushinkan.html'); } if(x > 38.88272548443688&& x < 38.88276724279492 && y > 139.83696833252904&& y < 139.8368489742279){ window.location.href('http://www.city.sakata.lg.jp/shisei/shisetsu/kyoiku/annai_dewayuushinkan.html'); } } var b = L.geoJson(nanshuujson, { style: function (feature) { return feature.properties; } }); var c = L.geoJson(dewajson, { style: function (feature) { return feature.properties; } }); var ai = L.geoJson(sevenjson, { style: function (feature) { return feature.properties; } }); var a = L.geoJson(daigaku, { style: function (feature) { return feature.properties; } }); var d = L.geoJson(sakatajson, { style: function (feature) { return feature.properties; } }); var e = L.geoJson(domonjson, { style: function (feature) { return feature.properties; } }); L.control.layers(null, {"出羽遊心館": c, "南洲神社":b, "セブンイレブン":ai, "東北公益文科大学":a, "酒田市国体記念館":d, "土門拳記念館":e }).addTo(koeki); function onError(err) { restN = "あと"+(--nTrial)+"回試行します。"; gpsmarker.setPopupContent("捕捉失敗:"+restN).openPopup(); if (nTrial <= 0) { navigator.geolocation.clearWatch(watchId); } } // STARTボタンに開始を仕込む document.getElementById("start").addEventListener("click", tryWatchGPS); // STOPボタンに停止を仕込む document.getElementById("stop").addEventListener("click", stopGPS); }, false);