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)); } var piyo=L.icon({ iconUrl:'piyopiyo.gif', className:'piyoko', iconSize:[30, 41], iconAnchor:[19, 19], popupAnchor: [-6, -10] }); function game(){ var threshold = 20000000; // 何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(),{icon:myIcon}).addTo(mymap); // var point=L.marker(mymap.getCenter(),{icon:piyo,zIndexOffset:999}).addTo(mymap); point.bindPopup("僕を水のあるところまでつれてって!").openPopup(); var stop = document.getElementById("stp"); var gps = document.getElementById("str"); 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, 3000); //ser.textContent = "5秒後にGPS取得…"; //tme.textContent= "周りに注意して使用しましょう!"; } } function clearTimer() { if (tmId) { clearTimeout(tmId); //nowTrying=nil; } } function countDown(){ if (doflag){ alert("周りに注意して使用して下さい"); //alert(""+naiyo[0]+"") tryGetGPS(); } else{ tryGetGPS(); doflag=true; tmId=null; } } function stopCountDown() { doflag=false; //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); //console.log(point); //point.setLatLng(latlng); //point.setPopupContent("あなたの位置はここです").openPopup().setLatLng(latlng); point.setLatLng(latlng); mymap.panTo(latlng); point.openPopup(); var lat = latlng.lat, lng = latlng.lng; //alert(iro); var g=1000000,d=0,i=0, come,name,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; console.log(d); if ( d <= g && k_type[i] == "Point"){ console.log(mkname[i]); g=d; near_point=ten[i]; come=naiyo[i]; name=mkname[i]; //color=iro[i]; } } if (g<= threshold){ navigator.vibrate(1000); near_point.bindPopup(come).openPopup(); point.bindPopup("わあい!"+name+"だぁ!とってもおいしいね!");//.openPopup(); 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); stop.addEventListener("click", stopCountDown, false); }; document.addEventListener("DOMContentLoaded", game, false);