|
| 1 | +import L from "https://code4sabae.github.io/leaflet-mjs/leaflet.mjs"; |
| 2 | + |
| 3 | +const link = document.createElement("link"); |
| 4 | +link.rel = "stylesheet"; |
| 5 | +link.href = "https://code4sabae.github.io/leaflet-mjs/leaflet.css"; |
| 6 | +document.body.appendChild(link); |
| 7 | +//link.onload = () => this.init(); |
| 8 | + |
| 9 | +const addGSILayer = (map) => { |
| 10 | + // set 国土地理院地図 https://maps.gsi.go.jp/development/ichiran.html |
| 11 | + L.tileLayer("https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png", { |
| 12 | + attribution: '<a href="https://maps.gsi.go.jp/development/ichiran.html">国土地理院</a>"', |
| 13 | + maxZoom: 18, |
| 14 | + }).addTo(map); |
| 15 | +}; |
| 16 | + |
| 17 | +const initMap = (mapid) => { |
| 18 | + const map = L.map(mapid) |
| 19 | + addGSILayer(map); |
| 20 | + |
| 21 | + // アイコン |
| 22 | + map.iconlayer = L.layerGroup(); |
| 23 | + map.iconlayer.addTo(map); |
| 24 | + map.addIcon = function(lat, lng, nameorparam, iconurl, iconwidth, iconheight) { |
| 25 | + var name = null; |
| 26 | + if (typeof nameorparam == "string") { |
| 27 | + name = nameorparam; |
| 28 | + } else if (nameorparam.name) { |
| 29 | + name = nameorparam.name; |
| 30 | + } |
| 31 | + var marker = null; |
| 32 | + if (iconurl) { |
| 33 | + if (!iconwidth) { |
| 34 | + iconwidth = 32; |
| 35 | + } |
| 36 | + if (!iconheight) { |
| 37 | + iconheight = iconwidth; |
| 38 | + } |
| 39 | + var icon = L.icon({ |
| 40 | + iconUrl: iconurl, |
| 41 | + iconSize: [ iconwidth, iconheight ], |
| 42 | + iconAnchor: [ iconwidth / 2, iconheight / 2 ] |
| 43 | + }); |
| 44 | + marker = L.marker([ lat, lng ], { |
| 45 | + title : name, |
| 46 | + icon : icon, |
| 47 | + }); |
| 48 | + } else { |
| 49 | + marker = L.marker([ lat, lng ], { title: name }); |
| 50 | + } |
| 51 | + if (typeof nameorparam == "function") { |
| 52 | + marker.on("click", function(e) { |
| 53 | + nameorparam(e, name) |
| 54 | + }); |
| 55 | + } else { |
| 56 | + marker.bindPopup( |
| 57 | + "<h2>" + name + "</h2>", |
| 58 | + { |
| 59 | + maxWidth: 500 |
| 60 | + } |
| 61 | + ); |
| 62 | + marker.on("click", function(e) { |
| 63 | + if (nameorparam && nameorparam.callback) |
| 64 | + nameorparam.callback(e, name) |
| 65 | + }); |
| 66 | + } |
| 67 | + this.iconlayer.addLayer(marker); |
| 68 | + return marker; |
| 69 | + } |
| 70 | + return map; |
| 71 | +}; |
| 72 | + |
| 73 | +const convertDMS2D = function(lat, lng) { |
| 74 | + const floor = Math.floor; |
| 75 | + const dms2d = (dms) => (floor(dms) + floor(dms * 100) % 100 / 60 + floor(dms * 10000) % 100 / (60 * 60) + floor(dms * 1000000) % 100 / (60 * 60 * 100)).toFixed(6); |
| 76 | + return [dms2d(lat), dms2d(lng)]; |
| 77 | +}; |
| 78 | +let watchid = null; |
| 79 | +const showHere = (map) => { |
| 80 | + if (watchid) { |
| 81 | + navigator.geolocation.clearWatch(watchid); |
| 82 | + } |
| 83 | + watchid = navigator.geolocation.watchPosition((gpos) => { |
| 84 | + const pos = [ gpos.coords.latitude, gpos.coords.longitude ]; |
| 85 | + console.log(pos); |
| 86 | + map.panTo(pos); |
| 87 | + navigator.geolocation.clearWatch(watchid); |
| 88 | + watchid = null; |
| 89 | + }, (e) => { |
| 90 | + alert("現在位置に取得に失敗!"); |
| 91 | + }); |
| 92 | +}; |
| 93 | + |
| 94 | +export { initMap, L, addGSILayer, convertDMS2D, showHere }; |
0 commit comments