Newer
Older
2022-yuuji-TwinMapWithImage / asahi.js
@Ito Koki Ito Koki on 6 Sep 2022 2 KB add GPS
/* 朝日中央地区: LatLng(38.671707, 139.795534), LatLng(38.572461, 139.880335) */
document.addEventListener("DOMContentLoaded", () => {
    var bounds = [[38.671707, 139.795534], [38.572461, 139.880335]]
    var center = [38.617121, 139.8417];
    var inizoom = 13;
    var infobox = document.getElementById("info");
    var mymapL = L.map("leftmap").setView(center, inizoom);
    L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
	attribution:
	'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(mymapL);
    mymapL.on('contextmenu', (e) => {
	// alert(e.latlng);
	let b=mymapL.getBounds(),
	    b1=b.getNorthWest(), b2=b.getSouthEast();
	infobox.innerText = `${e.latlng}, bound=${b1}, ${b2}`;
    });
    var mymapR = L.map("rightmap").setView(center, inizoom);
    // LatLng(38.903357, 139.813385), LatLng(38.878639, 139.834585)
    var leftBounds = mymapL.getBounds();
    L.imageOverlay("asahi-chuo2.jpg", bounds).addTo(mymapR);
    // L.imageOverlay("asahi-chuo2.jpg", leftBounds).addTo(mymapR);
    mymapR.setMaxBounds(bounds);
    function syncmap(srcmap, destmap) { // mapをlatLng中心にする
	var z=srcmap.getZoom(), c=srcmap.getCenter()
	destmap.setZoom(z); destmap.setView(c);
	infobox.innerText = `Center: ${c}, zm=${z}`;
    }

    var gpsmarker = L.marker(mymapL.getCenter()).addTo(mymapL);
    gpsmarker.bindPopup("START").openPopup();
    var gpsmarkerR = L.marker(mymapR.getCenter()).addTo(mymapR);
    var nTrial = 10;
    var watchId = null;

    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,{
		maximumAge: 0, timeout: 3000, enableHighAccuracy: true});
    }
    
    function onSuccess(pos) {
	var latlng = L.latLng([pos.coords.latitude, pos.coords.longitude]);
	mymapL.panTo(latlng);
	console.log(latlng);
	gpsmarker.setLatLng(latlng).setPopupContent(
	    "lat="+latlng.lat+",lng="+latlng.lng+"です"
	).openPopup();
	gpsmarkerR.setLatLng(latlng);
    }
    function onError(err) {
	restN = "あと"+(--nTrial)+"回試行します";
	gpsmarler.setPopupContent("補足失敗:"+restN).openPopup();
	if (nTrial <= 0) {
	    navigator.geolocation.clearWatch(watchId);
	}
    }
    
    mymapL.options.singleClickTimeout = 250;
    mymapL.on('singleclick', function(e) {
	if (true){
	    latlng = L.latLng(e.latlng);
	    gpsmarkerR.setLatLng(e.latlng);
	    mymapL.panTo(e.latlng);
	    console.log(e.latlng);
	    gpsmarker.setLatLng(e.latlng).setPopupContent(
		"lat="+latlng.lat+", lng="+latlng.lng+""
	    ).openPopup();
	}
    });
    
    mymapL.on('zoomend', (e) => {syncmap(mymapL, mymapR);});
    mymapL.on('moveend', (e) => {syncmap(mymapL, mymapR);});
    document.getElementById("start").addEventListener("click", tryWatchGPS);
    document.getElementById("stop").addEventListener("click", stopGPS);
});