// A test of maplibre-gl in a custom element class MapServiceArea extends HTMLElement { constructor() { super(); // Create a shadow DOM this.attachShadow({ mode: "open" }); // Initial render this.render(); this._map = null; // markers shown on the map this._markers = []; } // 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() { const centroid = JSON.parse(this.getAttribute("centroid")); const csv_file = this.getAttribute("csv-file"); const organization_id = this.getAttribute("organization-id"); const lat = Number(this.getAttribute("latitude") || 36.2); const lng = Number(this.getAttribute("longitude") || -119.2); const mapElement = this.shadowRoot.querySelector("#map"); const tegola = this.getAttribute("tegola"); const xmin = parseFloat(this.getAttribute("xmin")); const ymin = parseFloat(this.getAttribute("ymin")); const xmax = parseFloat(this.getAttribute("xmax")); const ymax = parseFloat(this.getAttribute("ymax")); const bounds = [ [xmin, ymin], [xmax, ymax], ]; console.log("fitting", bounds); this._map = new maplibregl.Map({ container: mapElement, center: centroid.coordinates, style: "https://tiles.stadiamaps.com/styles/alidade_smooth.json", }).fitBounds(bounds, { padding: { top: 10, bottom: 10, left: 10, right: 10 }, }); this._map.on("load", () => { this._map.addSource("tegola-nidus", { type: "vector", tiles: [`${tegola}maps/nidus/{z}/{x}/{y}?id=${organization_id}`], }); this._map.addLayer({ id: "service-area", source: "tegola-nidus", "source-layer": "service-area-bounds", type: "fill", paint: { "fill-opacity": 0.4, "fill-color": "#dc3545", }, }); }); } // Initial render of component render() { this.shadowRoot.innerHTML = `