Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 13x 13x 3x 3x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 1x 1x 4x 4x 5x 4x 4x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 1x 1x 3x 3x 4x 4x 4x 9x 1x 8x | import mapboxgl from "mapbox-gl";
import type { Map, MapLayerMouseEvent, MapboxGeoJSONFeature } from "mapbox-gl";
import type { Feature, Point } from "geojson";
import type { ArrivalLocation, StopLocation } from "@overworld/trimet-client";
import {
STOP_LOCATION_LAYER,
STOP_LOCATIONS_SOURCE,
CURRENT_LOCATION_CIRCLE_LAYER,
CURRENT_LOCATION_CIRCLE,
CURRENT_LOCATION_RADIUS_LAYER,
CURRENT_LOCATION_RADIUS
} from "./consts";
export type NearbyStopLocations = Record<number, Pick<StopLocation, "lat" | "lng" | "locid">>;
const stopHoverControls = new WeakMap<Map, {
dispose: () => void;
highlight: (stopId?: number) => void;
}>();
const STOP_DETAIL_MARKER = "stop-detail-marker";
export function showStopDetailMarker(mapBoxMap: Map, stop: Pick<ArrivalLocation, "lat" | "lng" | "desc" | "id">) {
mapBoxMap.addSource(STOP_DETAIL_MARKER, {
type: "geojson",
data: {
type: "Feature",
geometry: { type: "Point", coordinates: [stop.lng, stop.lat] },
properties: { locid: stop.id }
}
});
let popup: InstanceType<typeof mapboxgl.Popup>;
try {
mapBoxMap.addLayer({
id: STOP_DETAIL_MARKER,
type: "circle",
source: STOP_DETAIL_MARKER,
paint: {
"circle-color": "#ff0000",
"circle-emissive-strength": 1,
"circle-radius": 8,
"circle-stroke-color": "#ffffff",
"circle-stroke-width": 2
}
});
popup = new mapboxgl.Popup({
anchor: "bottom",
closeButton: false,
closeOnClick: false,
offset: 10
});
popup.setLngLat([stop.lng, stop.lat])
.setText(`${stop.desc}\nStop ID: ${stop.id}`)
.addTo(mapBoxMap);
} catch (error) {
popup?.remove();
Eif (mapBoxMap.getLayer(STOP_DETAIL_MARKER)) mapBoxMap.removeLayer(STOP_DETAIL_MARKER);
mapBoxMap.removeSource(STOP_DETAIL_MARKER);
throw error;
}
return () => {
popup.remove();
Eif (mapBoxMap.getLayer(STOP_DETAIL_MARKER)) mapBoxMap.removeLayer(STOP_DETAIL_MARKER);
Eif (mapBoxMap.getSource(STOP_DETAIL_MARKER)) mapBoxMap.removeSource(STOP_DETAIL_MARKER);
};
}
export function removeStopLocationLayers(mapBoxMap: Map) {
stopHoverControls.get(mapBoxMap)?.dispose();
stopHoverControls.delete(mapBoxMap);
let map = mapBoxMap;
console.log("removing stop location layers");
Eif (mapBoxMap.getLayer(STOP_LOCATION_LAYER)) {
map = mapBoxMap.removeLayer(STOP_LOCATION_LAYER);
}
Eif (mapBoxMap.getSource(STOP_LOCATIONS_SOURCE)) {
map = mapBoxMap.removeSource(STOP_LOCATIONS_SOURCE);
}
return map;
}
export function removeCurrentLocationMarkers(mapBoxMap: Map) {
console.log("removing current location markers");
if (mapBoxMap.getLayer(CURRENT_LOCATION_CIRCLE_LAYER)) {
mapBoxMap.removeLayer(CURRENT_LOCATION_CIRCLE_LAYER);
}
if (mapBoxMap.getSource(CURRENT_LOCATION_CIRCLE)) {
mapBoxMap.removeSource(CURRENT_LOCATION_CIRCLE);
}
if (mapBoxMap.getLayer(CURRENT_LOCATION_RADIUS_LAYER)) {
mapBoxMap.removeLayer(CURRENT_LOCATION_RADIUS_LAYER);
}
if (mapBoxMap.getSource(CURRENT_LOCATION_RADIUS)) {
mapBoxMap.removeSource(CURRENT_LOCATION_RADIUS);
}
}
export function setNearbyStops(
mapBoxMap: Map,
stopLocations: NearbyStopLocations,
routeIds: string[],
handleStopMarkerClick: (data: MapboxGeoJSONFeature) => void
) {
const isLoaded = mapBoxMap?.loaded();
console.log("is map loaded", isLoaded);
console.log("setting nearby stops", stopLocations, routeIds);
const features: Array<Feature<Point>> = Object.values(stopLocations).map(stopLocation => {
return {
geometry: {
type: "Point",
coordinates: [stopLocation.lng, stopLocation.lat]
},
properties: {
locid: stopLocation.locid,
routeids: routeIds.join(",") // Ensure routeids are included
},
type: "Feature"
};
});
mapBoxMap.addSource(STOP_LOCATIONS_SOURCE, {
data: {
type: "FeatureCollection",
features
},
type: "geojson"
});
mapBoxMap.addLayer({
id: STOP_LOCATION_LAYER,
paint: {
"circle-color": "#4264fb",
"circle-emissive-strength": 1,
"circle-radius": 8,
"circle-stroke-color": "#ffffff",
"circle-stroke-width": 2
},
source: STOP_LOCATIONS_SOURCE,
type: "circle"
});
function handleClick(e: MapLayerMouseEvent) {
const feature = e.features?.[0];
if (feature?.geometry.type !== "Point") {
return;
}
mapBoxMap.flyTo({
center: [feature.geometry.coordinates[0], feature.geometry.coordinates[1]]
});
mapBoxMap.setZoom(16);
handleStopMarkerClick(feature);
}
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
let highlightedStop: number | undefined;
let hoveredMapStop: number | undefined;
let hoveredLongitude: number;
function showPopup() {
const id = hoveredMapStop ?? highlightedStop;
const stop = id === undefined ? undefined : stopLocations[id];
if (!stop) {
popup.remove();
return;
}
const coordinates: [number, number] = [stop.lng, stop.lat];
const longitude = hoveredMapStop === undefined
? mapBoxMap.getCenter().lng : hoveredLongitude;
while (Math.abs(longitude - coordinates[0]) > 180) {
coordinates[0] += longitude > coordinates[0] ? 360 : -360;
}
const popupHtml = `<strong>Stop ID</strong>: ${stop.locid}<br><strong>Routes</strong>: ${routeIds.join(", ")}`;
popup
.setLngLat(coordinates)
.setHTML(popupHtml)
.addTo(mapBoxMap);
}
function handleMouseEnter(e: MapLayerMouseEvent) {
mapBoxMap.getCanvas().style.cursor = "pointer";
hoveredMapStop = Number(e.features?.[0]?.properties.locid);
hoveredLongitude = e.lngLat.lng;
showPopup();
}
function handleMouseLeave() {
mapBoxMap.getCanvas().style.cursor = "";
hoveredMapStop = undefined;
showPopup();
}
mapBoxMap.on("click", STOP_LOCATION_LAYER, handleClick);
mapBoxMap.on("mouseenter", STOP_LOCATION_LAYER, handleMouseEnter);
mapBoxMap.on("mouseleave", STOP_LOCATION_LAYER, handleMouseLeave);
stopHoverControls.set(mapBoxMap, {
dispose: () => {
popup.remove();
mapBoxMap.getCanvas().style.cursor = "";
mapBoxMap.off("click", STOP_LOCATION_LAYER, handleClick);
mapBoxMap.off("mouseenter", STOP_LOCATION_LAYER, handleMouseEnter);
mapBoxMap.off("mouseleave", STOP_LOCATION_LAYER, handleMouseLeave);
},
highlight: (stopId?: number) => {
highlightedStop = stopId;
showPopup();
}
});
return mapBoxMap;
}
export function highlightNearbyStop(mapBoxMap: Map, stopId?: number) {
updateStopMarkerColor(mapBoxMap, stopId === undefined ? undefined : String(stopId), "#ff0000");
stopHoverControls.get(mapBoxMap)?.highlight(stopId);
}
export function updateStopMarkerColor(
mapBoxMap: Map,
stopId: string | undefined,
color: string
) {
if (!mapBoxMap?.getLayer(STOP_LOCATION_LAYER)) {
return;
}
mapBoxMap.setPaintProperty(STOP_LOCATION_LAYER, "circle-color",
stopId === undefined ? "#4264fb" : [
"case",
["==", ["get", "locid"], parseInt(stopId, 10)],
color,
"#4264fb"
]);
}
|