|
| 1 | +import PanoIcon, { SIZE as ICON_SIZE } from "./pano-icon.js"; |
| 2 | +import { MAPTILER_KEY } from "./config.js"; |
| 3 | + |
| 4 | + |
| 5 | +function addLayers(map) { |
| 6 | + let osm = L.tileLayer(`https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png`, { |
| 7 | + maxZoom: 19, |
| 8 | + attribution: `© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>` |
| 9 | + }); |
| 10 | + |
| 11 | + let topo = L.tileLayer(`https://api.maptiler.com/maps/topo/{z}/{x}/{y}.png?key=${MAPTILER_KEY}`, { |
| 12 | + tileSize: 512, |
| 13 | + zoomOffset: -1, |
| 14 | + minZoom: 1, |
| 15 | + attribution: `© <a href="https://www.maptiler.com/copyright/">MapTiler</a>, <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>` |
| 16 | + }); |
| 17 | + |
| 18 | + let satellite = L.tileLayer(`https://api.maptiler.com/tiles/satellite-v2/{z}/{x}/{y}.jpg?key=${MAPTILER_KEY}`, { |
| 19 | + tileSize: 512, |
| 20 | + zoomOffset: -1, |
| 21 | + minZoom: 1, |
| 22 | + attribution: `© <a href="https://www.maptiler.com/copyright/">MapTiler</a>, <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>` |
| 23 | + }); |
| 24 | + |
| 25 | + let layers = { |
| 26 | + "Base + Terrain": topo, |
| 27 | + "OSM": osm, |
| 28 | + "Satellite": satellite |
| 29 | + }; |
| 30 | + L.control.layers(layers).addTo(map); |
| 31 | + map.addLayer(topo); |
| 32 | +} |
| 33 | + |
| 34 | +const dateFormat = new Intl.DateTimeFormat([], {dateStyle:"medium", timeStyle:"short"}); |
| 35 | + |
| 36 | +export default class PanoMap extends HTMLElement { |
| 37 | + #map; |
| 38 | + #markers = new Map(); |
| 39 | + #panoIcons = new Map(); |
| 40 | + |
| 41 | + constructor() { |
| 42 | + super(); |
| 43 | + } |
| 44 | + |
| 45 | + connectedCallback() { |
| 46 | + const map = L.map(this); |
| 47 | + addLayers(map); |
| 48 | + this.#map = map; |
| 49 | + } |
| 50 | + |
| 51 | + showItems(items) { |
| 52 | + this.#markers.clear(); |
| 53 | + |
| 54 | + let group = L.markerClusterGroup({showCoverageOnHover:false, animate:false, maxClusterRadius:40}); |
| 55 | + items.map(item => this.#buildMarker(item)).forEach(m => group.addLayer(m)); |
| 56 | + this.#map.addLayer(group); |
| 57 | + |
| 58 | + let bounds = group.getBounds(); |
| 59 | + if (bounds.isValid()) { |
| 60 | + this.#map.fitBounds(bounds); |
| 61 | + } else { |
| 62 | + this.#map.setView([0, 0], 2); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + activate(item, options) { |
| 67 | + if (options.center) { this.#map.setView([item["GPSLatitude"], item["GPSLongitude"]], 17); } |
| 68 | + |
| 69 | + for (let [i, panoIcon] of this.#panoIcons.entries()) { |
| 70 | + panoIcon.classList.toggle("active", i == item); |
| 71 | + } |
| 72 | + |
| 73 | + let marker = this.#markers.get(item); |
| 74 | + if (!marker._icon) { marker.__parent.spiderfy(); } |
| 75 | + |
| 76 | + if (options.popup) { marker.openPopup(); } |
| 77 | + } |
| 78 | + |
| 79 | + highlight(item) { |
| 80 | + for (let [i, panoIcon] of this.#panoIcons.entries()) { |
| 81 | + panoIcon.classList.toggle("highlight", i == item); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + syncSize() { // fixme resizeobserver |
| 86 | + this.#map.invalidateSize(); |
| 87 | + } |
| 88 | + |
| 89 | + #dispatch(type, item) { |
| 90 | + let event = new CustomEvent(type, {detail:{item}}); |
| 91 | + this.dispatchEvent(event); |
| 92 | + } |
| 93 | + |
| 94 | + #buildPopup(item) { |
| 95 | + let frag = document.createDocumentFragment(); |
| 96 | + |
| 97 | + let name = document.createElement("a"); |
| 98 | + let url = new URL(location.href); |
| 99 | + url.hash = item["SourceFile"]; |
| 100 | + name.href = url.href; |
| 101 | + name.textContent = item["ImageDescription"] || "n/a"; |
| 102 | + name.addEventListener("click", _ => this.#dispatch("pano-click", item)); |
| 103 | + |
| 104 | + let date = document.createElement("div"); |
| 105 | + date.append(dateFormat.format(new Date(item["CreateDate"] * 1000))); |
| 106 | + |
| 107 | + let altitude = document.createElement("div"); |
| 108 | + altitude.append(`Altitude: ${item["GPSAltitude"]} m`); |
| 109 | + |
| 110 | + frag.append(name, date, altitude); |
| 111 | + return frag; |
| 112 | + } |
| 113 | + |
| 114 | + #buildMarker(item) { |
| 115 | + let panoIcon = new PanoIcon(); |
| 116 | + let iconSize = [ICON_SIZE, ICON_SIZE]; |
| 117 | + let popupAnchor = [0, -ICON_SIZE/2]; |
| 118 | + let icon = L.divIcon({html:panoIcon, popupAnchor, iconSize, className:""}); |
| 119 | + let marker = L.marker([item["GPSLatitude"], item["GPSLongitude"]], {title:item["ImageDescription"] || "", icon}); |
| 120 | + |
| 121 | + item.panoIcon = panoIcon; |
| 122 | + marker.bindPopup(() => this.#buildPopup(item)); |
| 123 | + |
| 124 | + panoIcon.addEventListener("mouseenter", _ => this.#dispatch("pano-over", item)); |
| 125 | + panoIcon.addEventListener("mouseleave", _ => this.#dispatch("pano-out", item)); |
| 126 | + |
| 127 | + this.#markers.set(item, marker); |
| 128 | + this.#panoIcons.set(item, panoIcon); |
| 129 | + |
| 130 | + return marker; |
| 131 | + } |
| 132 | +} |
| 133 | +customElements.define("pano-map", PanoMap); |
0 commit comments