Newer
Older
stamp-2022 / koki / koryo.js
@Ito Koki Ito Koki on 15 Sep 2022 2 KB バグの修正
/* 公益大範囲: latlng(38.894780,139.818229), latlng(38.891346,139.821018)*/
document.addEventListener("DOMContentLoaded", () => {
    var bounds = [[38.894780,139.818229],[38.891346,139.821018]]
    var mymap = L.map("mymap").setView([38.893, 139.819],17);
    L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
	attribution:
	'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contrivutors'
    }).addTo(mymap);
    L.imageOverlay("koeki-map.png", bounds).addTo(mymap);
    mymap.setMaxBounds(bounds);

    var gpsmarker = L.marker(mymap.getCenter()).addTo(mymap);
    gpsmarker.bindPopup("START").openPopup();

    var nTrial = 10;
    var watchId = null;

    let point = [[38.894097, 139.819253],[38.892467, 139.819615]];

    let titen = [];
    let marker = [];
    let group = [];
    let judge = [];
    let img = [];
    for (var x = 0; x<2;x++){
	titen[x] = L.latLng(point[x]);
	marker[x] = L.marker(titen[x]);
    };
    
    group = L.layerGroup(marker);
    group.addTo(mymap);
 
    
    function stopGPS() {
	console.log("watchId="+watchId);
	if (watchId != null) {
	    navigator.geolocation.clearWatch(watchId);
	    gpsmarker.setPopupContent("止めました");
	}
	watchId = null;
    }

    function tryWatchGPS() {
	stopGPS();
	watchId = navigator.geolocation.watchPosition(
	    onSuccess,onError,{
		macimumAge: 0, timeout: 3000, enavleHifhAccuracy: true});
    }
    
    function onSuccess(pos) {
	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();
	for (var i = 0; i < 2; i++) {
	    if (latlng.distanceTo(titen[i]) < 30) {
		if (!judge[i]){
		    judge[i] = true;
		    alert("スタンプゲット!!");
		    let img = document.getElementById('image${i}');
		    img.src = "sengoku.png";
		}
	    }
	}
    }
    
    function onError(err) {
	restN = "あと"+(--nTrial)+"回試行します";
	gpsmarker.setPopupContent("補足失敗:"+restN).openPopup();
	if (nTrial <= 0) {
	    navigator.geolocation.clearWatch(watchId);
	}
    }

    mymap.options.singleClickTimeout = 250;
    mymap.on('singleclick', function(e) {
	if (true){
	    latlng = L.latLng(e.latlng);
	    gpsmarker.setLatLng(e.latlng);
	    mymap.panTo(e.latlng);
	    console.log(e.latlng);
	    gpsmarker.setLatLng(e.latlng).setPopupContent(
		"lat="+latlng.lat+", lng="+latlng.lng+""
	    ).openPopup();
	}
	for (var i = 0; i < 2; i++) {
	    if (latlng.distanceTo(titen[i]) < 30) {
		if (!judge[i]){
		    judge[i] = true;
		    alert("スタンプゲット!!");
		    img[i] = document.getElementById('image${i}');
		    img[i].src = "sengoku.png";
		}
	    }
	}
    });
    
    document.getElementById("start").addEventListener("click", tryWatchGPS);
    document.getElementById("stop").addEventListener("click", stopGPS);
}, false);