(function myself() {
    var map1, map2;		// map1=LEFT, map2=RIGHT
    var base1, base2;
    function syncOn() {
	map1.on('moveend', samerange);
	map2.on('moveend', samerange);
    }
    function syncOff() {
	map1.off('moveend', samerange);
	map2.off('moveend', samerange);
    }
    function samerange(me) {
	var map = me.target, tmap;
	tmap = (map == map1 ? map2 : map1);
	try {
	    syncOff();
	    tmap.setZoom(map.getZoom());	// setZoom() First!!
	    tmap.fitBounds(map.getBounds());
	} finally {
	    syncOn();
	}
    }
    // Function for uMap-Style markdown conversion
    function wikiRepl(s) {	// uMap風マークダウン変換
	return s.replace(/{{(.*)\|([0-9][0-9]*)}}/g,
			 '<img class="popup" src="$1" width="$2">').
	    replace(/{{(.*)}}/g, '<img class="popup" src="$1">').
	    replace(/\[\[(.*)\|(.*)\]\]/g, '<a href="$1">$2</a>').
	    replace(/\[\[(.*)\]\]/g, '<a href="$1">$1</a>').
	    replace(/^### (.*)/, '<h4>$1</h4>').
	    replace(/^## (.*)/, '<h3>$1</h4>').
	    replace(/^# (.*)/, '<h2>$1</h4>');
    }
    var lastPopup;
    function syncPopup(ev) {
	var t = ev.target;
	if (!t.getLatLng) {	// When CLOSING
	    try {
		map1.off('popupclose', syncPopup);
		map2.off('popupclose', syncPopup);
		lastPopup.closePopup();
	    } finally {
		map1.on('popupclose', syncPopup);
		map2.on('popupclose', syncPopup);
	    }
	    return;
	}
	let latlng = t.getLatLng(),
	    ll = [latlng.lng, latlng.lat];
	var popups = markerConstruction[ll];
	if (!popups) return;

	try {
	    for (let p of popups) {
		p.off('popupopen', syncPopup);
	    }
	    for (let p of popups) {
		if (p != ev.target)
		    if (ev.type=="popupopen")
			lastPopup=p.openPopup();
		    else 
			p.closePopup();
	    }
	} finally {
	    for (let p of popups) {
		 p.on('popupopen', syncPopup);
	    }
	}
    }
    var markerCollection = {};
    function markerConstruction(feature, layer) {
	let p = feature.properties,	// "properties" in GeoJSON format
	    g = feature.geometry;
	if (p) {
	    let name = p.name, desc = p.description;
	    let popup = "<h3>" + name + "</h3>" + wikiRepl(desc||"");
	    layer.bindPopup(popup);
	    layer.on('popupopen', syncPopup);
	}
	if (g && g.type == "Point" && g.coordinates) {
	    let co = g.coordinates;
	    markerConstruction[co] || (markerConstruction[co]=[])
	    markerConstruction[co].push(layer)
	    console.log(co);
	}
    }
    function newGejsonLayer() {
	return L.geoJson(null, {	// Layer for omnivore
	    onEachFeature: markerConstruction});
    }

    function init() {
	map1 = L.map("map1").setView([38.891, 139.824], 13);
	map2 = L.map("map2").setZoom(map1.getZoom()).fitBounds(map1.getBounds());
	baseLayer = {};
	baseLayer['OpenStreetMap'] = base1 = L.tileLayer(
	    'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
		attribution:
		'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
		maxZoom: 20, maxNativeZoom: 18
	    });
	baseLayer['国土地理院(標準)'] = base2 = L.tileLayer(
	    'https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png', {
		attribution:
		'&copy; <a href="https://maps.gsi.go.jp/development/ichiran.html">国土地理院</a>',
		maxZoom: 20, maxNativeZoom: 18
	    });
	baseLayer['簡易空中写真(国土地理院2004年)'] = L.tileLayer(
	    // 'https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png', {
	    'https://cyberjapandata.gsi.go.jp/xyz/airphoto/{z}/{x}/{y}.png', {
		attribution:
		'&copy; <a href="https://maps.gsi.go.jp/development/ichiran.html">国土地理院</a>',
		maxZoom: 20, maxNativeZoom: 18
	    });
	var gj1 = omnivore.geojson("test-s1.geojson", null, newGejsonLayer());
	var gj2 = omnivore.geojson("test-s2.geojson", null, newGejsonLayer());
	var ovl1 = {"今昔": gj1}, ovl2 = {"今昔": gj2};
	Object.create(base1).addTo(map1);	// クローンしないと
	Object.create(base2).addTo(map2);	// 2つの地図で取り合いが起こる
	var bl2 = Object.create(baseLayer);
	L.control.layers(baseLayer, ovl1).addTo(map1);
	gj1.addTo(map1);
	L.control.layers(bl2, ovl2).addTo(map2);
	gj2.addTo(map2);
	// gj2.on('ready', function(){console.log(JSON.stringify(gj2.toGeoJSON()))});
	syncOn();
	map1.on('popupclose', syncPopup);
	map2.on('popupclose', syncPopup);
    }
    window.onload = init;
})();
