2026-01-14 20:14:58 +00:00
|
|
|
// A map that can be used to locate a single point by setting its location explicitly
|
|
|
|
|
// or by allowing the user to move a marker.
|
|
|
|
|
class MapLocator extends HTMLElement {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
// Create a shadow DOM
|
2026-03-03 20:27:33 +00:00
|
|
|
this.attachShadow({ mode: "open" });
|
2026-01-14 20:14:58 +00:00
|
|
|
|
|
|
|
|
// Initial render
|
|
|
|
|
this.render();
|
|
|
|
|
|
|
|
|
|
// markers shown on the map. Should be none or 1, generally.
|
2026-01-24 19:55:09 +00:00
|
|
|
this._markers = [];
|
2026-01-14 20:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lifecycle: when element is added to the DOM
|
|
|
|
|
connectedCallback() {
|
|
|
|
|
// Initialize the map when the element is added to the DOM
|
|
|
|
|
setTimeout(() => this._initializeMap(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disconnectedCallback() {
|
|
|
|
|
if (this._map) {
|
|
|
|
|
this._map.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_initializeMap() {
|
|
|
|
|
console.log("Setting up the map...");
|
2026-01-21 17:50:43 +00:00
|
|
|
const apiKey = this.getAttribute("api-key");
|
2026-03-03 20:27:33 +00:00
|
|
|
const lat = Number(this.getAttribute("latitude") || 36.2);
|
|
|
|
|
const lng = Number(this.getAttribute("longitude") || -119.2);
|
|
|
|
|
const zoom = Number(this.getAttribute("zoom") || 15);
|
2026-01-14 20:14:58 +00:00
|
|
|
|
2026-01-24 19:55:09 +00:00
|
|
|
const mapElement = this.shadowRoot.querySelector("#map");
|
2026-03-03 20:27:33 +00:00
|
|
|
this._map = new maplibregl.Map({
|
2026-01-21 17:50:43 +00:00
|
|
|
container: mapElement,
|
2026-01-14 20:14:58 +00:00
|
|
|
center: {
|
|
|
|
|
lat: lat,
|
|
|
|
|
lng: lng,
|
|
|
|
|
},
|
2026-03-03 20:27:33 +00:00
|
|
|
style: "https://tiles.stadiamaps.com/styles/alidade_smooth.json",
|
2026-01-14 20:14:58 +00:00
|
|
|
zoom: zoom,
|
|
|
|
|
});
|
2026-01-24 19:55:09 +00:00
|
|
|
/*
|
2026-03-03 20:27:33 +00:00
|
|
|
map.addControl(new maplibregl.GeolocateControl({
|
2026-01-14 20:14:58 +00:00
|
|
|
positionOptions: {
|
|
|
|
|
enableHighAccuracy: true
|
|
|
|
|
},
|
|
|
|
|
trackUserLocation: true,
|
|
|
|
|
showUserHeading: true
|
|
|
|
|
}));
|
2026-03-03 20:27:33 +00:00
|
|
|
map.addControl(new maplibregl.NavigationControl());
|
2026-01-24 19:55:09 +00:00
|
|
|
*/
|
2026-02-03 17:01:39 +00:00
|
|
|
this._map.on("click", (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
console.log("internal click", e);
|
2026-03-03 20:27:33 +00:00
|
|
|
this.dispatchEvent(
|
|
|
|
|
new CustomEvent("click", {
|
|
|
|
|
bubbles: true,
|
|
|
|
|
composed: true, // Allows event to cross shadow DOM boundary
|
|
|
|
|
detail: {
|
|
|
|
|
lngLat: e.lngLat,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-02-03 17:01:39 +00:00
|
|
|
});
|
2026-01-24 19:55:09 +00:00
|
|
|
this._map.on("load", () => {
|
|
|
|
|
console.log("map loaded");
|
2026-03-03 20:27:33 +00:00
|
|
|
this.dispatchEvent(
|
|
|
|
|
new CustomEvent("load", {
|
|
|
|
|
bubbles: true,
|
|
|
|
|
composed: true, // Allows event to cross shadow DOM boundary
|
|
|
|
|
detail: {
|
|
|
|
|
map: this,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-01-14 20:14:58 +00:00
|
|
|
});
|
2026-01-30 22:13:22 +00:00
|
|
|
this._map.on("zoomend", (e) => {
|
2026-03-03 20:27:33 +00:00
|
|
|
this.dispatchEvent(
|
|
|
|
|
new CustomEvent("zoomend", {
|
|
|
|
|
bubbles: true,
|
|
|
|
|
composed: true,
|
|
|
|
|
detail: e,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-01-30 22:13:22 +00:00
|
|
|
});
|
2026-01-14 20:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initial render of component
|
|
|
|
|
render() {
|
|
|
|
|
this.shadowRoot.innerHTML = `
|
|
|
|
|
<style>
|
2026-03-03 20:27:33 +00:00
|
|
|
@import url("//unpkg.com/maplibre-gl@5.0.1/dist/maplibre-gl.css");
|
2026-01-14 20:14:58 +00:00
|
|
|
#map {
|
2026-03-03 20:52:02 +00:00
|
|
|
height: 100%;
|
2026-01-14 20:14:58 +00:00
|
|
|
width:100%;
|
|
|
|
|
}
|
|
|
|
|
#map img {
|
|
|
|
|
max-width: none;
|
|
|
|
|
min-width: 0px;
|
|
|
|
|
height: auto;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
2026-03-03 20:52:02 +00:00
|
|
|
<div id="map"></div>
|
2026-01-14 20:14:58 +00:00
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 22:13:22 +00:00
|
|
|
GetZoom() {
|
|
|
|
|
return this._map.getZoom();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 21:28:07 +00:00
|
|
|
JumpTo(args) {
|
2026-01-14 20:14:58 +00:00
|
|
|
this._map.jumpTo(args);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 17:22:32 +00:00
|
|
|
PanTo(coords, options) {
|
|
|
|
|
this._map.panTo(coords, options);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 21:28:07 +00:00
|
|
|
SetMarker(coords) {
|
2026-01-14 20:14:58 +00:00
|
|
|
console.log("Setting map marker", coords);
|
|
|
|
|
this._markers.forEach((marker) => marker.remove());
|
|
|
|
|
|
2026-03-03 20:27:33 +00:00
|
|
|
const marker = new maplibregl.Marker({
|
2026-01-14 20:14:58 +00:00
|
|
|
color: "#FF0000",
|
2026-03-03 20:27:33 +00:00
|
|
|
draggable: true,
|
|
|
|
|
})
|
|
|
|
|
.setLngLat(coords)
|
|
|
|
|
.addTo(this._map);
|
|
|
|
|
marker.on("dragend", (e) => {
|
2026-01-14 20:14:58 +00:00
|
|
|
const markerDraggedEvent = new CustomEvent("markerdragend", {
|
|
|
|
|
detail: {
|
2026-03-03 20:27:33 +00:00
|
|
|
marker: marker,
|
|
|
|
|
},
|
2026-01-14 20:14:58 +00:00
|
|
|
});
|
2026-01-30 21:38:29 +00:00
|
|
|
this.dispatchEvent(markerDraggedEvent);
|
2026-01-14 20:14:58 +00:00
|
|
|
});
|
|
|
|
|
this._markers = [marker];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 20:27:33 +00:00
|
|
|
customElements.define("map-locator", MapLocator);
|