-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMap.tsx
More file actions
281 lines (252 loc) · 9.52 KB
/
Map.tsx
File metadata and controls
281 lines (252 loc) · 9.52 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import React, { useEffect, useMemo, useRef, useState } from "react";
import MapGL, {
Layer,
NavigationControl,
Popup,
Source,
type LayerProps,
type MapRef,
} from "react-map-gl/mapbox";
import type { FeatureCollection, Point } from "geojson";
import { Location } from "../../types";
interface MapProps {
locations: Location[];
zoom: number;
viewKey?: string;
}
function toRadians(degrees: number): number {
return degrees * (Math.PI / 180);
}
function toDegrees(radians: number): number {
return radians * (180 / Math.PI);
}
// calculate the center of all locations on the map view to center the map
function getGeographicCenter(
locations: Location[]
): [number, number] {
if (!locations.length) return [39.9515, -75.191];
let x = 0;
let y = 0;
let z = 0;
locations.forEach((coord) => {
const lat = toRadians(coord.lat);
const lon = toRadians(coord.lng);
x += Math.cos(lat) * Math.cos(lon);
y += Math.cos(lat) * Math.sin(lon);
z += Math.sin(lat);
});
const total = locations.length;
x /= total;
y /= total;
z /= total;
const centralLongitude = toDegrees(Math.atan2(y, x));
const centralSquareRoot = Math.sqrt(x * x + y * y);
const centralLatitude = toDegrees(Math.atan2(z, centralSquareRoot));
return [centralLatitude ? centralLatitude : 39.9515, centralLongitude ? centralLongitude : -75.1910];
}
function separateOverlappingPoints(points: Location[], offset = 0.0001) {
const validPoints = points.filter((p) => p.lat !== null && p.lng !== null) as Location[];
// group points by coordinates
const groupedPoints: Record<string, Location[]> = {};
validPoints.forEach((point) => {
const key = `${point.lat},${point.lng}`;
(groupedPoints[key] ||= []).push(point);
});
// adjust overlapping points
const adjustedPoints = Object.values(groupedPoints).flatMap((group) =>
group.length === 1
? group // no adjustment needed if class in map view doesnt share locations with others
: group.map((point, index) => {
/*
At a high level, if there are multiple classes in map view that have the exact same location,
we try to evenly distribute them around a "circle" centered on the original location.
The size of the circle is determined by offset.
*/
const angle = (2 * Math.PI * index) / group.length;
return {
...point,
lat: point.lat! + offset * Math.cos(angle),
lng: point.lng! + offset * Math.sin(angle),
};
})
);
// include points with null values
return adjustedPoints;
}
function Map({ locations, zoom, viewKey }: MapProps) {
const mapRef = useRef<MapRef | null>(null);
const mapboxToken = process.env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN;
const mapboxStyleId = process.env.NEXT_PUBLIC_MAPBOX_STYLE_ID || "mapbox/streets-v12";
const [cursor, setCursor] = useState<string>("");
const [autoCenter, setAutoCenter] = useState(true);
const [selected, setSelected] = useState<{
longitude: number;
latitude: number;
id?: string;
room?: string;
start?: number;
end?: number;
color?: string;
} | null>(null);
const mapStyle = useMemo(() => {
if (mapboxStyleId.startsWith("mapbox://")) return mapboxStyleId;
return `mapbox://styles/${mapboxStyleId}`;
}, [mapboxStyleId]);
const center = useMemo(() => getGeographicCenter(locations), [locations]);
const points = useMemo(() => separateOverlappingPoints(locations), [locations]);
const markerGeoJson = useMemo<
FeatureCollection<Point, { color?: string; id?: string; room?: string; start?: number; end?: number }>
>(() => {
return {
type: "FeatureCollection",
features: points.map((p) => ({
type: "Feature",
properties: {
color: p.color,
id: p.id,
room: p.room,
start: p.start,
end: p.end,
},
geometry: { type: "Point", coordinates: [p.lng, p.lat] },
})),
};
}, [points]);
const markerLayer = useMemo<LayerProps>(
() => ({
id: "pcp-course-markers",
type: "circle",
paint: {
"circle-radius": 6,
"circle-color": ["coalesce", ["get", "color"], "#878ED8"],
"circle-stroke-color": "rgba(0,0,0,0.35)",
"circle-stroke-width": 2,
},
}),
[]
);
const formatTime = (t?: number) => {
if (t == null) return "";
const hours24 = Math.floor(t);
const minutes = Math.round((t % 1) * 100);
const period = hours24 >= 12 ? "PM" : "AM";
const hours12 = hours24 % 12 === 0 ? 12 : hours24 % 12;
return `${hours12}:${minutes.toString().padStart(2, "0")} ${period}`;
};
// When the day/view changes, re-enable auto-centering.
useEffect(() => {
setAutoCenter(true);
setSelected(null);
}, [viewKey]);
useEffect(() => {
if (!mapRef.current || !autoCenter) return;
mapRef.current.flyTo({
center: [center[1], center[0]],
zoom,
essential: true,
});
}, [autoCenter, center, zoom]);
if (!mapboxToken) {
return (
<div
style={{
height: "100%",
width: "100%",
display: "flex",
alignItems: "center",
justifyContent: "center",
color: "#6b7280",
fontSize: "0.9rem",
background: "#f9fafb",
borderRadius: 8,
}}
>
Missing `NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN`
</div>
);
}
return (
<MapGL
ref={mapRef}
mapboxAccessToken={mapboxToken}
mapStyle={mapStyle}
initialViewState={{
latitude: center[0],
longitude: center[1],
zoom,
pitch: 0,
bearing: 0,
}}
style={{ height: "100%", width: "100%" }}
attributionControl
dragRotate
touchPitch
maxPitch={70}
interactiveLayerIds={["pcp-course-markers"]}
cursor={cursor}
onDragStart={() => setAutoCenter(false)}
onZoomStart={() => setAutoCenter(false)}
onRotateStart={() => setAutoCenter(false)}
onPitchStart={() => setAutoCenter(false)}
onMouseMove={(e) => {
const hovering = (e.features?.length || 0) > 0;
setCursor(hovering ? "pointer" : "");
}}
onClick={(e) => {
const f = e.features?.[0];
if (!f || f.geometry.type !== "Point") {
setSelected(null);
return;
}
const [lng, lat] = f.geometry.coordinates as [number, number];
const props = (f.properties || {}) as Record<string, unknown>;
// Center the map on the clicked marker (keep current zoom).
setAutoCenter(false);
mapRef.current?.flyTo({
center: [lng, lat],
essential: true,
});
setSelected({
longitude: lng,
latitude: lat,
id: typeof props.id === "string" ? props.id : undefined,
room: typeof props.room === "string" ? props.room : undefined,
start: typeof props.start === "number" ? props.start : undefined,
end: typeof props.end === "number" ? props.end : undefined,
color: typeof props.color === "string" ? props.color : undefined,
});
}}
>
<NavigationControl showCompass showZoom visualizePitch position="top-left" />
<Source id="pcp-course-markers-source" type="geojson" data={markerGeoJson}>
<Layer {...markerLayer} />
</Source>
{selected && (
<Popup
longitude={selected.longitude}
latitude={selected.latitude}
anchor="top"
closeButton
closeOnClick={false}
onClose={() => setSelected(null)}
maxWidth="260px"
>
<div style={{ fontSize: "0.85rem", lineHeight: 1.25 }}>
{selected.id && (
<div style={{ fontWeight: 700, marginBottom: 4 }}>
{selected.id.replace(/-/g, " ")}
</div>
)}
{(selected.start != null || selected.end != null) && (
<div style={{ marginBottom: 2 }}>
{formatTime(selected.start)}{selected.end != null ? `–${formatTime(selected.end)}` : ""}
</div>
)}
{selected.room && <div>{selected.room}</div>}
</div>
</Popup>
)}
</MapGL>
);
};
export default React.memo(Map);