    document.addEventListener("DOMContentLoaded", () => {
        var mymap = L.map("gpsmap").setView([38.916631, 139.836495], 16);
        L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    	attribution:
    	'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> \
    		contributors'
        }).addTo(mymap);
	console.log("1");
	pgload(mymap);
	
        var gpsmarker = L.marker(mymap.getCenter()).addTo(mymap);
        gpsmarker.bindPopup("START").openPopup();
        var G = L.latLng([38.893434,139.819310]);
        console.log("2");
	
        var nTrial = 100
        var watchId = null;
	var latlng = L.latLng(mymap.getCenter());
	
        function stopGPS() {
    	console.log("watchId="+watchId);
    	if (watchId != null) {	
    	    navigator.geolocation.clearWatch(watchId);
    	    document.getElementById("title").textContent = "stop";
    	    gpsmarker.setPopupContent("ストップ");
    	}
    	watchId = null;
        }
        function tryWatchGPS() {
    	watchId = navigator.geolocation.watchPosition(
    	    onSuccess, onError,{
    		maximumAge: 0, timeout: 3000, enableHighAccuracy: true});
    	document.getElementById("title").textContent = "START!!";
	}
        function onSuccess(pos) {
	    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.lng, y = latlng.lat;
	    console.log("4");
     if  (latlng.distanceTo(G)<30/*y >38.919061 && y <38.914019&& x >139.833823&& x <139.840411*/) {
    	    alert("あと一歩だね?");
    	 stopGPS();
	 } else if (x > 38.905745 && x < 38.905198 && y > 139.849150 && y < 139.849262) {
	    alert("ここがみずほぅ!");
	    stopGPS();
        }
        }


	
        function onError(err) {
    	restN = "あと"+(--nTrial)+"回試行します";
    	gpsmarker.setPopupContent("捕捉失敗:"+restN).openPopup();
    	if (nTrial <= 0) {
    	    navigator.geolocation.clearWatch(watchId);
    	}
	    console.log("5");
	}
        // START
	
	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);
	    }
	});
        document.getElementById("start").addEventListener("click", tryWatchGPS);
        
        document.getElementById("stop").addEventListener("click", stopGPS);
    }, false);
