Newer
Older
2021-Fuki-second / map / test / test.js
/*I made Map N38.913E139.909 and 15level!*/
(() => {
var mymap = L.map("mymap").setView([38.913, 139.909], 15);
L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
	attribution:
	'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a>contributors'
}).addTo(mymap);

/*I made a operation area !*/
var root = [			
    [38.918127,139.911900],
    [38.919208,139.900126],
    [38.902379,139.898469],
    [38.901944,139.916015]
];
var ste = {			
    color: "orange",		
    opacity: 0.4,		
    weight: 9
};
var plobj = L.polygon(root, ste).addTo(mymap);

/*I put enermy's hot area */
var alpha = L.circle([38.913949,139.907390], {radius: 100, color: "red"}).addTo(mymap);
var bravo = L.circle([38.911120,139.911692], {radius: 100, color: "red"}).addTo(mymap);
var charly = L.circle([38.907009,139.903832], {radius: 100, color: "purple"}).addTo(mymap);

alpha.bindPopup("Lv.0~10");
bravo.bindPopup("Lv.0~10");
charly.bindPopup("Lv.11~20");

/*I put GPS charsers!!*/
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.lat, y = latlng.lng;
	if(x > 38.892960 && x < 38.893068 && y > 139.817843 && y <139.818283){
	    alert("?????!!");
	    stopGPS();
	}else {
	    document.getElementByID("title").textContent
		="????????!";
	}
	// ????? ????? latlng.lat ? latlng.lng ?
	// if?????????????????????GOAL!??
	// ?????????????
    }
    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);
})();