function distance(x1, y1, x2, y2) { // ヒュベニ式による距離概算 rx = 6378137; // 赤道半径(m) WGS84 ry = 6356752.314; // 極半径(m) WGS84 e2=(rx*rx-ry*ry)/rx/rx; // 離心率 E^2 dx = (x2-x1)*Math.PI/180; // 経度の差をラジアン変換 dy = (y2-y1)*Math.PI/180; // 緯度の差をラジアン変換 my = (y1+y2)/2.0*Math.PI/180; // 緯度の平均をラジアン変換 w = Math.sqrt(1-e2*Math.sin(my)*Math.sin(my)); m = rx*(1-e2)/Math.pow(w,3); // 子午線曲率半径 n = rx/w; // 卯酉線曲率半径 return Math.sqrt(Math.pow(dy*m,2) + Math.pow(dx*n*Math.cos(my),2)); } function game(){ var threshold = 10; // 何m接近をゴールとするか var timerInterval = 10000; // GPS失敗で何秒後に再取得か var tmId = null; var i = 0;//, countD = document.getElementById("countdown"); //var info = document.getElementById("info"); var point = L.marker(mymap.getCenter()); // document.getElementById("switch1").checked=false; var gps = document.getElementById("off"); var nowTrying = null; var doflag=true; function tryGetGPS() { clearTimer(); //ser.textContent = "GPS取得..." nowTrying = navigator.geolocation.getCurrentPosition( onSuccess, onError,{ maximumAge: 0, timeout: 3000, enableHighAccuracy: true}) } function onemore() { if (nowTrying || tmId) { //info.textContent = "既にGPS取得試行or待機中です"; } else { startTimer(); //tm.textContent=""+naiyo[0]+""; } } function startTimer() { if (tmId==null && doflag){ tmId = setTimeout(tryGetGPS, 5000); //ser.textContent = "5秒後にGPS取得…"; //tme.textContent= "周りに注意して使用しましょう!"; } } function clearTimer() { if (tmId) { clearTimeout(tmId); //nowTrying=nil; } } function countDown(){ if (doflag){ alert("周りに注意して使用して下さい"); gps.id="on"; //console.log(gps.id); var stop = document.getElementById("on"); stop.addEventListener("click", stopCountDown, false); //alert(""+naiyo[0]+"") tryGetGPS(); } else{ tryGetGPS(); doflag=true; tmId=null; } } function stopCountDown() { doflag=false; stop.id="off"; gps.addEventListener("click", countDown, false); //console.log(stop.id); //countD.textContent = "stop!"; //ser.innerHTML = "startボタンをもう一度押すと始まります"; } function onSuccess(pos) { nowTrying = null; tmId=null; mymap.setZoom(18); var latlng = L.latLng([pos.coords.latitude, pos.coords.longitude]); //ser.textContent = "現在の位置は"+latlng+"です。"; mymap.panTo(latlng); //point.setPopupContent("あなたの位置はここです").openPopup().setLatLng(latlng); point.bindPopup("あなたの位置はここです").setLatLng(latlng).addTo(mymap); var lat = latlng.lat, lng = latlng.lng; //alert(iro); var g=100000,d=0,i=0, come,color,near_point; //var m=feature.geometry.coordinates; for(i=0; i<kasyo.length; i++){ var kiken = L.latLng(kasyo[i][1],kasyo[i][0]); var klat =kiken.lat, klng=kiken.lng; d=distance(klng,klat,lng,lat); d=Math.floor(d*100)/100; if ( d <= g && iro[i]=="red" && k_type[i] == "Point" || d <= g && iro[i]=="Red" && k_type[i] == "Point"|| d <= g && iro[i]=="yellow" && k_type[i] == "Point" || d <= g && iro[i]=="Gold" && k_type[i] == "Point"|| d <= g && iro[i]=="" && k_type[i] == "Point"|| d <= g && iro[i]=="Yellow" && k_type[i] == "Point"){ g=d; near_point=ten[i]; //come=naiyo[i]; //color=iro[i]; } } if (g<= threshold){ //alert(come); //near_point.openPopup(); //info.style.color="red"; //info.innerHTML =""+come+""; navigator.vibrate(1000); onemore(); } else{ //tm.textContent=""+g+""; //info.innerHTML = "一番近い危険箇所への距離は"+Math.round(g*100)/100+"m"; // 表示 //info.style.color="black"; onemore(); } } function onError(err) { // 失敗時 nowTrying = null; //ser.textContent = "現在位置の取得失敗.10秒後にもう一回"; tmId = setTimeout(tryGetGPS, timerInterval); } gps.addEventListener("click", countDown, false); }; document.addEventListener("DOMContentLoaded", game, false);