(() => {
function myAtr() {
  /*MAPset*/
  var gsi = L.tileLayer('//cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="https://maps.gsi.go.jp">国土地理院地図</a> contributors'
    });
  var osm = L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
     attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    });
  var BingAir = new L.BingLayer("AvRR1iCvcIBp2R9fSTjK7FSHYDT0zpGCWT9PoyO7gHt9nEFBOcROZuuTnXjhWOig", {
     type: "Aerial", culture: "ja"
    });
  var gpsmap = L.map("seek", {zoomControl: false });
  gsi.addTo(gpsmap);
  gpsmap.setView([38.892919, 139.818078], 16);
  var baseMaps = {
    "地理院地図" : gsi,
    "OpenStreetMap" : osm,
    "BingMap航空写真" : BingAir
  };

  L.control.layers(baseMaps).addTo(gpsmap);
  L.control.zoom({ position: 'topleft' }).addTo(gpsmap);
  L.control.scale({ position: 'bottomright' }).addTo(gpsmap);

  gpsmap.on('mousemove', onMapMousemove)
  var laloinfo = L.control({ position: "bottomleft" });
  laloinfo.onAdd = function (map) {
    this.ele = L.DomUtil.create('div', "infostyle");
    this.ele.id = "latlondiv";
    this.ele.style.visibility = "hidden";
    this.ele.onmousemove = function (e) { e.stopPropagation() };
    return this.ele;
    };
  laloinfo.addTo(gpsmap);
  function onMapMousemove(e) {
    var box = document.getElementById("latlondiv");
    var llnum = "緯度：" + e.latlng.lat.toFixed(6) + "<br>" +
                "経度：" + e.latlng.lng.toFixed(6);
                box.innerHTML = llnum;
                box.style.visibility = "visible";
    };
  /*ICON-POPUP-set*/
  var reaIcon = L.icon({
    className: 'popicon1',
    iconSize: [30,30],
    iconUrl: 'mark01.png'
  });

  popicon1 = L.marker([38.920325,139.850970], {icon:reaIcon}).addTo(gpsmap);

  //popicon.bindPopup(popup,{autoClose:false}).openPopup();

  var repeat = 5000;

  var pulseIcon = L.icon.pulse(gpsmap.getCenter(), {
      color: '#00ff00',
      iconSize: [25,25]
    });
  
  var gpsmarker = L.marker([50,15],{icon: pulseIcon}).addTo(gpsmap);

  gpsmarker.bindPopup("更新中").openPopup();

  /*GPS-set*/
  var reTry = 5;
  var timeId = null;

  function interRupt() {
    console.log("timeId="+timeId);
    if (timeId != null) {
        navigator.geolocation.clearWatch(timeId);
        document.getElementById("title").textContent = "stop";
        gpsmarker.setPopupContent("取得を停止しました");
    }
    timeId = null;
  }
  function getGPS() {
    interRupt();
    gpsmarker.bindPopup("位置情報を取得中").openPopup()
    navigator.geolocation.getCurrentPosition(hitting, missing, {
      maximumAge: 0,
      timeout: 10000,
      enableHighAccuracy: true
    });
  }

  function hitting(pos){
    var latlng = L.latLng([pos.coords.latitude, pos.coords.longitude]);
	  gpsmap.flyTo(latlng);
	  gpsmarker.setPopupContent(
	    "現在地"
      ).openPopup().setLatLng(latlng);
    
    var ido = latlng.lat, kedo = latlng.lng;
      if (ido > 38.919787 && ido < 38.920901 && kedo > 139.850217 && kedo < 139.851665) {
        popicon.bindPopupContent(popupContent).openPopup(gpsmap);
      }
  }

  function missing(err){
    var attention = [
      err.message,
      '位置情報の取得に失敗しました',
      'あと'+(--reTry)+'回試行します。'
    ];
    if (reTry > 0) {
	    window.alert(attention[err.code]);
	    timeId = setTimeout(getGPS, repeat);
    } else {
	    var suspention = '<span style="color: red;">位置情報の取得を中断します。</span>';
	    window.alert(suspention);
    }
  }

  getGPS();

  /*HTML-link*/
  document.getElementById("start").addEventListener("click", getGPS);
  document.getElementById("stop").addEventListener("click", interRupt);

}
document.addEventListener("DOMContentLoaded", myAtr, false);
})();