2026-01-15 20:25:00 +00:00
|
|
|
var map = null;
|
|
|
|
|
// 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 MapAggregate extends HTMLElement {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
// Create a shadow DOM
|
2026-02-10 18:08:34 +00:00
|
|
|
this.attachShadow({ mode: "open" });
|
2026-01-15 20:25:00 +00:00
|
|
|
|
2026-02-10 18:08:34 +00:00
|
|
|
this._markers = [];
|
2026-01-15 20:25:00 +00:00
|
|
|
// Initial render
|
|
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lifecycle: watch these attributes for changes
|
|
|
|
|
static get observedAttributes() {
|
2026-02-10 18:08:34 +00:00
|
|
|
return [
|
|
|
|
|
"api-key",
|
|
|
|
|
"latitude",
|
|
|
|
|
"longitude",
|
|
|
|
|
"organization-id",
|
|
|
|
|
"tegola",
|
|
|
|
|
"zoom",
|
|
|
|
|
];
|
2026-01-15 20:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lifecycle: respond to attribute changes
|
|
|
|
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
|
|
|
// Only handle if map exists and values actually changed
|
|
|
|
|
if (!this._map || oldValue === newValue) return;
|
2026-02-10 18:08:34 +00:00
|
|
|
|
|
|
|
|
if (name === "api-key") {
|
2026-01-15 20:25:00 +00:00
|
|
|
this._apiKey = newValue;
|
|
|
|
|
}
|
2026-02-10 18:08:34 +00:00
|
|
|
|
|
|
|
|
if (name === "latitude" || name === "longitude") {
|
|
|
|
|
if (this.hasAttribute("latitude") && this.hasAttribute("longitude")) {
|
|
|
|
|
const lat = Number(this.getAttribute("latitude"));
|
|
|
|
|
const lng = Number(this.getAttribute("longitude"));
|
2026-01-15 20:25:00 +00:00
|
|
|
this._map.setCenter([lat, lng]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 18:08:34 +00:00
|
|
|
if (name === "organization-id") {
|
2026-01-15 20:25:00 +00:00
|
|
|
this._organizationID = newValue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 18:08:34 +00:00
|
|
|
if (name === "tegola") {
|
2026-01-15 20:25:00 +00:00
|
|
|
this._tegola = newValue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 18:08:34 +00:00
|
|
|
if (name === "zoom") {
|
2026-01-15 20:25:00 +00:00
|
|
|
this._map.setZoom(Number(newValue));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-10 18:08:34 +00:00
|
|
|
|
2026-01-15 20:25:00 +00:00
|
|
|
_initializeMap() {
|
|
|
|
|
const lat = Number(this.getAttribute("latitude") || 36.2);
|
|
|
|
|
const lng = Number(this.getAttribute("longitude") || -119.2);
|
|
|
|
|
const organization_id = Number(this.getAttribute("organization-id") || 0);
|
2026-02-10 18:08:34 +00:00
|
|
|
const tegola = this.getAttribute("tegola");
|
2026-01-15 20:25:00 +00:00
|
|
|
const zoom = Number(this.getAttribute("zoom") || 15);
|
|
|
|
|
|
|
|
|
|
const mapElement = this.shadowRoot.querySelector("#map");
|
2026-02-16 18:19:50 +00:00
|
|
|
this._map = new maplibregl.Map({
|
2026-01-15 20:25:00 +00:00
|
|
|
container: mapElement,
|
|
|
|
|
center: {
|
|
|
|
|
lat: lat,
|
|
|
|
|
lng: lng,
|
|
|
|
|
},
|
2026-02-16 18:19:50 +00:00
|
|
|
style: "https://tiles.stadiamaps.com/styles/alidade_smooth.json",
|
2026-01-15 20:25:00 +00:00
|
|
|
zoom: zoom,
|
|
|
|
|
});
|
2026-02-10 18:08:34 +00:00
|
|
|
this._map.on("load", () => {
|
|
|
|
|
this._map.addSource("tegola", {
|
|
|
|
|
type: "vector",
|
|
|
|
|
tiles: [
|
|
|
|
|
`${tegola}maps/nidus/{z}/{x}/{y}?organization_id=${organization_id}`,
|
|
|
|
|
],
|
2026-01-15 20:25:00 +00:00
|
|
|
});
|
2026-02-10 18:08:34 +00:00
|
|
|
this._map.addLayer({
|
|
|
|
|
id: "mosquito_source",
|
|
|
|
|
type: "fill",
|
|
|
|
|
filter: [
|
|
|
|
|
"==",
|
|
|
|
|
["zoom"],
|
|
|
|
|
["+", 2, ["to-number", ["get", "resolution"]]],
|
|
|
|
|
],
|
|
|
|
|
source: "tegola",
|
|
|
|
|
"source-layer": "mosquito_source",
|
|
|
|
|
paint: {
|
|
|
|
|
"fill-opacity": 0.4,
|
|
|
|
|
"fill-color": "#dc3545",
|
|
|
|
|
},
|
2026-01-15 20:25:00 +00:00
|
|
|
});
|
2026-02-10 18:08:34 +00:00
|
|
|
this._map.addLayer({
|
|
|
|
|
id: "service_request",
|
|
|
|
|
type: "fill",
|
|
|
|
|
filter: [
|
|
|
|
|
"==",
|
|
|
|
|
["zoom"],
|
|
|
|
|
["+", 2, ["to-number", ["get", "resolution"]]],
|
|
|
|
|
],
|
|
|
|
|
source: "tegola",
|
|
|
|
|
"source-layer": "service_request",
|
|
|
|
|
paint: {
|
|
|
|
|
"fill-opacity": 0.4,
|
|
|
|
|
"fill-color": "#ffc107",
|
|
|
|
|
},
|
2026-01-15 20:25:00 +00:00
|
|
|
});
|
2026-02-10 18:08:34 +00:00
|
|
|
this._map.addLayer({
|
|
|
|
|
id: "trap",
|
|
|
|
|
type: "fill",
|
|
|
|
|
filter: [
|
|
|
|
|
"==",
|
|
|
|
|
["zoom"],
|
|
|
|
|
["+", 2, ["to-number", ["get", "resolution"]]],
|
|
|
|
|
],
|
|
|
|
|
source: "tegola",
|
|
|
|
|
"source-layer": "trap",
|
|
|
|
|
paint: {
|
|
|
|
|
"fill-opacity": 0.4,
|
|
|
|
|
"fill-color": "#0dcaf0",
|
|
|
|
|
},
|
2026-01-15 20:25:00 +00:00
|
|
|
});
|
2026-02-16 18:19:50 +00:00
|
|
|
this._map.on("mouseenter", "mosquito_source", (e) => {
|
|
|
|
|
this._map.getCanvas().style.cursor = "pointer";
|
2026-01-15 22:15:29 +00:00
|
|
|
});
|
2026-02-16 18:19:50 +00:00
|
|
|
this._map.on("mouseleave", "mosquito_source", (e) => {
|
|
|
|
|
this._map.getCanvas().style.cursor = "";
|
2026-01-15 22:15:29 +00:00
|
|
|
});
|
2026-02-16 18:19:50 +00:00
|
|
|
const _handleClick = (e) => {
|
|
|
|
|
const feature = e.features[0];
|
|
|
|
|
const coordinates = feature.geometry.coordinates.slice();
|
|
|
|
|
const properties = feature.properties;
|
|
|
|
|
this.dispatchEvent(
|
|
|
|
|
new CustomEvent("cell-click", {
|
|
|
|
|
bubbles: true,
|
|
|
|
|
composed: true, // Allows event to cross shadow DOM boundary
|
|
|
|
|
detail: {
|
|
|
|
|
cell: properties.cell,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
this._map.on("click", "mosquito_source", _handleClick);
|
|
|
|
|
this._map.on("click", "service_request", _handleClick);
|
|
|
|
|
this._map.on("click", "trap", _handleClick);
|
2026-01-15 20:25:00 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initial render of component
|
|
|
|
|
render() {
|
|
|
|
|
this.shadowRoot.innerHTML = `
|
|
|
|
|
<style>
|
2026-02-16 18:19:50 +00:00
|
|
|
@import url("//unpkg.com/maplibre-gl@5.0.1/dist/maplibre-gl.css");
|
2026-01-15 20:25:00 +00:00
|
|
|
.map-container {
|
|
|
|
|
background-color: #e9ecef;
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
|
|
|
|
|
height: 500px;
|
|
|
|
|
margin-top: 20px;
|
2026-02-10 18:08:34 +00:00
|
|
|
position: relative;
|
2026-01-15 20:25:00 +00:00
|
|
|
}
|
|
|
|
|
#map {
|
2026-02-10 18:08:34 +00:00
|
|
|
position: absolute;
|
|
|
|
|
top: 0;
|
|
|
|
|
bottom: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
height: 100%;
|
|
|
|
|
width: 100%
|
2026-01-15 20:25:00 +00:00
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div id="map-container" class="map-container">
|
|
|
|
|
<div id="map"></div>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jumpTo(args) {
|
|
|
|
|
this._map.jumpTo(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setMarker(coords) {
|
|
|
|
|
console.log("Setting map marker", coords);
|
|
|
|
|
this._map.jumpTo({
|
|
|
|
|
center: coords,
|
|
|
|
|
zoom: 14,
|
|
|
|
|
});
|
|
|
|
|
this._markers.forEach((marker) => marker.remove());
|
|
|
|
|
|
2026-02-16 18:19:50 +00:00
|
|
|
const marker = new maplibregl.Marker({
|
2026-01-15 20:25:00 +00:00
|
|
|
color: "#FF0000",
|
2026-02-10 18:08:34 +00:00
|
|
|
draggable: true,
|
|
|
|
|
})
|
|
|
|
|
.setLngLat(coords)
|
|
|
|
|
.addTo(this._map);
|
|
|
|
|
marker.on("dragend", function (e) {
|
2026-01-15 20:25:00 +00:00
|
|
|
const markerDraggedEvent = new CustomEvent("markerdragend", {
|
|
|
|
|
detail: {
|
2026-02-10 18:08:34 +00:00
|
|
|
marker: marker,
|
|
|
|
|
},
|
2026-01-15 20:25:00 +00:00
|
|
|
});
|
|
|
|
|
mapContainer.dispatchEvent(markerDraggedEvent);
|
|
|
|
|
});
|
|
|
|
|
this._markers = [marker];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 18:08:34 +00:00
|
|
|
customElements.define("map-aggregate", MapAggregate);
|