|
| 1 | +import { computed, effect } from "@preact/signals-core"; |
| 2 | +import { bbox } from "@turf/turf"; |
| 3 | +import maplibregl, { GeoJSONSource } from "maplibre-gl"; |
| 4 | +import "maplibre-gl/dist/maplibre-gl.css"; |
| 5 | +import { notes, visible, focus } from "../state"; |
| 6 | + |
| 7 | +type Locations = GeoJSON.FeatureCollection< |
| 8 | + GeoJSON.Point, |
| 9 | + { id: number; text: string } |
| 10 | +>; |
| 11 | + |
| 12 | +export const noteLocations = computed<Locations>(() => ({ |
| 13 | + type: "FeatureCollection", |
| 14 | + features: notes.value.map((note) => ({ |
| 15 | + type: "Feature", |
| 16 | + properties: { |
| 17 | + id: note.id, |
| 18 | + text: note.text, |
| 19 | + }, |
| 20 | + geometry: { |
| 21 | + type: "Point", |
| 22 | + coordinates: [note.lon, note.lat], |
| 23 | + }, |
| 24 | + })), |
| 25 | +})); |
| 26 | + |
| 27 | +export const visibleLocations = computed<Locations>(() => { |
| 28 | + const all = noteLocations.value; |
| 29 | + |
| 30 | + const features = all.features.filter((p) => |
| 31 | + visible.value.has(p.properties.id) |
| 32 | + ); |
| 33 | + |
| 34 | + return { ...all, features }; |
| 35 | +}); |
| 36 | + |
| 37 | +export const focusLocation = computed<Locations>(() => { |
| 38 | + const all = noteLocations.value; |
| 39 | + |
| 40 | + const features = all.features.filter((p) => p.properties.id === focus.value); |
| 41 | + |
| 42 | + return { ...all, features }; |
| 43 | +}); |
| 44 | + |
| 45 | +export class CycloopsMap extends HTMLDivElement { |
| 46 | + connectedCallback() { |
| 47 | + console.log("connected map", this); |
| 48 | + |
| 49 | + this.style.position = "fixed"; |
| 50 | + this.style.left = |
| 51 | + this.style.top = |
| 52 | + this.style.right = |
| 53 | + this.style.bottom = |
| 54 | + "0"; |
| 55 | + this.style.zIndex = "-1"; |
| 56 | + |
| 57 | + const map = new maplibregl.Map({ |
| 58 | + container: this, |
| 59 | + style: "https://tiles.openfreemap.org/styles/positron", |
| 60 | + center: [0, 0], |
| 61 | + zoom: 1, |
| 62 | + interactive: false, |
| 63 | + }); |
| 64 | + |
| 65 | + map.on("load", () => { |
| 66 | + // add a blank feature collection |
| 67 | + map.addSource("notes", { |
| 68 | + type: "geojson", |
| 69 | + data: { |
| 70 | + type: "FeatureCollection", |
| 71 | + features: [], |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + map.addLayer({ |
| 76 | + id: "notes_markers", |
| 77 | + type: "circle", |
| 78 | + source: "notes", |
| 79 | + paint: { |
| 80 | + "circle-radius": 5, |
| 81 | + "circle-color": "#f08", |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + map.addSource("focus", { |
| 86 | + type: "geojson", |
| 87 | + data: { |
| 88 | + type: "FeatureCollection", |
| 89 | + features: [], |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + map.addLayer({ |
| 94 | + id: "focus_markers", |
| 95 | + type: "circle", |
| 96 | + source: "focus", |
| 97 | + paint: { |
| 98 | + "circle-radius": 10, |
| 99 | + "circle-color": "#08f", |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + effect(() => { |
| 104 | + if (visibleLocations.value.features.length === 0) return; |
| 105 | + |
| 106 | + map.getSource<GeoJSONSource>("notes")?.setData(visibleLocations.value); |
| 107 | + |
| 108 | + const bounds = bbox(visibleLocations.value) as [ |
| 109 | + number, |
| 110 | + number, |
| 111 | + number, |
| 112 | + number |
| 113 | + ]; |
| 114 | + |
| 115 | + map.fitBounds(bounds, { padding: 100, speed: 5 }); |
| 116 | + }); |
| 117 | + |
| 118 | + effect(() => { |
| 119 | + map.getSource<GeoJSONSource>("focus")?.setData(focusLocation.value); |
| 120 | + }); |
| 121 | + }); |
| 122 | + } |
| 123 | +} |
0 commit comments