Newer
Older
rain_disaster_map / koeki / catgps.js~
@natto natto on 26 Oct 2018 3 KB koeki
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]
});

var piyo=L.icon({
    iconUrl:'piyopiyo.gif',
    className:'piyoko',
    iconSize:[30, 41],
    iconAnchor:[19, 19],
    popupAnchor: [-6, -10]
});

var piyoX=L.icon({
    iconUrl:'emoji_xx.png',
    className:'piyoko',
    iconSize:[30, 41],
    iconAnchor:[19, 19],
    popupAnchor: [-6, -10]
});

var ini=L.marker([38.97372749369379,139.94388515335632]).addTo(mymap);
function timeStart(){
    //console.log("unko");
    var res=confirm("okでゲームスタート!");
    if( res == true ) {
	var time=setInterval(mk_cicle(time),180);
	
    }else {
	timeStart();
    }
    //clearTimeout(circles);
}; 

function game(){
   
    var timerInterval = 10000;	// GPS失敗で何秒後に再取得か
    var tmId = null;
    var i = 0;//, countD = document.getElementById("countdown");
   
    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: 1000, 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, 1000);
    	    //ser.textContent = "5秒後にGPS取得…";
	    //tme.textContent= "周りに注意して使用しましょう!";
    	}
    }
    
    function clearTimer() {
     	if (tmId) {
	    clearTimeout(tmId);
	    //nowTrying=nil;
	}
    }

    function countDown(){
	if (doflag){
	    alert("周りに注意して使用して下さい");
	    tryGetGPS();
	    timeStart();
	} else{
	    tryGetGPS();
	    doflag=true;
	    tmId=null;
	}
    }

    function stopCountDown() {
	doflag=false;
    }
   
    function onSuccess(pos) {
	nowTrying = null;
	tmId=null;
	mymap.setZoom(5);
	var latlng = L.latLng([pos.coords.latitude, pos.coords.longitude]);

	point.setLatLng(latlng);
	mymap.panTo(latlng);
	
	var lat = latlng.lat, lng = latlng.lng;

	var g=1000000,d=0,i=0, come,name,near_point;
	var dist=latlng.distanceTo(circle);
	var dist=ini._latlng.distanceTo(circle);
	var panco=panko[panko.length-1];
	
	if(panco<=dist){
	    onemore();
	}else if(100<=panco){
	    point.bindPopup("助かったぴよー").openPopup();
	    stopCountDown();
	}else{
	    point.setIcon(piyoX).bindPopup("焼き鳥になっちゃったぴよー…").openPopup();
	    alert("追いつかれちゃった…");
	    stopCountDown();
	}
    }	

    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);