-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflightprogressmap.html
More file actions
323 lines (277 loc) · 9.91 KB
/
flightprogressmap.html
File metadata and controls
323 lines (277 loc) · 9.91 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Rotating & Draggable Plane</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<!-- Load Mapbox GL JS and Turf.js libraries -->
<script src="https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css" rel="stylesheet" />
<script src="https://unpkg.com/@turf/turf@6.5.0/turf.min.js"></script>
<!-- Load custom font -->
<link href="https://fonts.googleapis.com/css2?family=Oswald&display=swap" rel="stylesheet" />
<style>
/* Basic page layout and font */
body { margin: 0; padding: 0; font-family: 'Oswald', sans-serif; }
/* Map container */
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
/* Main title at top center */
#map-title {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
font-size: 24px;
font-weight: bold;
color: #fff;
text-shadow: 1px 1px 3px #000;
z-index: 2;
}
/* Byline directly under the title */
#byline {
position: absolute;
top: 42px;
width: 100%;
text-align: center;
font-size: 14px;
color: #eee;
text-shadow: 1px 1px 2px #000;
z-index: 2;
}
/* Flight info panel on the top-left */
#info-box {
position: absolute;
top: 70px;
left: 10px;
background: rgba(255,255,255,0.9);
padding: 10px;
border-radius: 6px;
font-size: 14px;
z-index: 1;
}
/* Progress bar container and fill bar */
#progress-bar {
position: absolute;
top: 240px;
left: 10px;
width: 200px;
height: 10px;
background: #eee;
border-radius: 5px;
overflow: hidden;
z-index: 1;
}
#progress-fill {
height: 100%;
width: 0%;
background: #007cbf;
}
/* Start and pause buttons */
#start-button, #pause-button {
position: absolute;
top: 260px;
padding: 6px 12px;
font-size: 14px;
border: none;
background: #007cbf;
color: white;
border-radius: 4px;
cursor: pointer;
z-index: 1;
}
#start-button { left: 10px; }
#pause-button { left: 110px; }
/* Slider below the buttons */
#slider-container {
position: absolute;
top: 300px;
left: 10px;
z-index: 1;
width: 200px;
}
#slider { width: 100%; }
/* Attribution box */
#attribution {
position: absolute;
bottom: 10px;
left: 10px;
font-size: 12px;
background: rgba(255,255,255,0.8);
padding: 4px 8px;
border-radius: 4px;
z-index: 1;
}
#attribution a {
color: #000;
text-decoration: none;
}
#attribution a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<!-- Page title and byline -->
<div id="map-title">Flight Tracker: Seattle to Kansas City</div>
<div id="byline">By Hailey Rosquist</div>
<!-- Mapbox map container -->
<div id="map"></div>
<!-- Info box showing live flight stats -->
<div id="info-box">
<strong>Flight Status for AS296</strong><br>
Time: <span id="time">0 min</span><br>
Distance: <span id="distance">0 mi</span><br>
Altitude: <span id="altitude">0 ft</span><br>
Speed: <span id="speed">0 mph</span><br>
Lat/Lng: <span id="coords">-</span>
</div>
<!-- Progress bar UI -->
<div id="progress-bar"><div id="progress-fill"></div></div>
<!-- Flight controls -->
<button id="start-button">Start Flight</button>
<button id="pause-button">Pause</button>
<!-- Slider to move through flight -->
<div id="slider-container">
<input type="range" min="0" max="200" value="0" id="slider" />
</div>
<!-- Icon credit -->
<div id="attribution">
<a href="https://www.flaticon.com/free-icon/plane_870143" target="_blank" rel="noopener noreferrer">
Plane icon created by Freepik - Flaticon
</a>
</div>
<script>
// Mapbox access token
mapboxgl.accessToken = 'pk.eyJ1IjoiaHJvc3F1aXN0IiwiYSI6ImNtOTF1ZWhoZzA0eWcyam42cWszanM5ZnAifQ.cEBrgBlFHQg9Utrk2ovNeg';
// Map
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/satellite-streets-v12',
center: [-108, 43.5],
zoom: 4
});
// Flight path from SEA to MCI
const start = [-122.307, 47.4515], end = [-94.725, 39.2992];
const totalFlightMinutes = 211;
const totalDistance = turf.distance(turf.point(start), turf.point(end), { units: 'miles' });
const avgSpeed = (totalDistance / totalFlightMinutes) * 60;
const numPoints = 200, pathCoords = [];
const line = turf.lineString([start, end]);
// Generate path points
for (let i = 0; i <= numPoints; i++) {
const pt = turf.along(line, (i / numPoints) * totalDistance, { units: 'miles' });
pathCoords.push(pt.geometry.coordinates);
}
// State variables for animation
let animationIndex = 0, paused = false, animationTimeout;
let planeMarker;
// Update flight display and plane location
function updateFlight(index) {
const coord = pathCoords[index];
const next = pathCoords[index + 1] || coord;
const bearing = turf.bearing(turf.point(coord), turf.point(next));
const dist = turf.distance(turf.point(start), turf.point(coord), { units: 'miles' });
const progress = dist / totalDistance;
const elapsedMin = Math.floor(progress * totalFlightMinutes);
const altitude = progress < 0.2 ? (progress / 0.2) * 35000 :
progress > 0.8 ? ((1 - progress) / 0.2) * 35000 : 35000;
const speed = progress < 0.2 ? (progress / 0.2) * avgSpeed :
progress > 0.8 ? ((1 - progress) / 0.2) * avgSpeed : avgSpeed;
document.getElementById('time').textContent = `${elapsedMin} min`;
document.getElementById('distance').textContent = `${dist.toFixed(1)} mi`;
document.getElementById('altitude').textContent = `${Math.round(altitude)} ft`;
document.getElementById('speed').textContent = `${Math.round(speed)} mph`;
document.getElementById('coords').textContent = `${coord[1].toFixed(2)}, ${coord[0].toFixed(2)}`;
document.getElementById('progress-fill').style.width = `${(progress * 100).toFixed(1)}%`;
document.getElementById('slider').value = index;
// Update map plane position
map.getSource('plane').setData({
type: 'FeatureCollection',
features: [{ type: 'Feature', geometry: { type: 'Point', coordinates: coord }, properties: { rotation: bearing } }]
});
planeMarker.setLngLat(coord);
}
// Animate plane
function animateFlight() {
if (animationIndex >= pathCoords.length || paused) return;
updateFlight(animationIndex++);
animationTimeout = setTimeout(animateFlight, 80);
}
// Load map layers and plane marker
map.on('load', () => {
// Add flight line layer
const arcLine = turf.lineString(pathCoords);
map.addSource('flight', { type: 'geojson', data: arcLine });
map.addLayer({
id: 'flight-line',
type: 'line',
source: 'flight',
layout: { 'line-cap': 'round', 'line-join': 'round' },
paint: { 'line-color': '#fff', 'line-width': 4 }
});
// Airport popups
new mapboxgl.Popup({ closeOnClick: false }).setLngLat(start).setHTML("<strong>Seattle-Tacoma Intl (SEA)</strong>").addTo(map);
new mapboxgl.Popup({ closeOnClick: false }).setLngLat(end).setHTML("<strong>Kansas City Intl (MCI)</strong>").addTo(map);
// Load and register plane icon
map.loadImage('https://cdn-icons-png.flaticon.com/512/870/870143.png', (err, image) => {
if (err) throw err;
map.addImage('plane-icon', image);
// Create plane source + layer
map.addSource('plane', {
type: 'geojson',
data: { type: 'FeatureCollection', features: [] }
});
map.addLayer({
id: 'plane-layer',
type: 'symbol',
source: 'plane',
layout: {
'icon-image': 'plane-icon',
'icon-size': 0.08,
'icon-rotate': ['get', 'rotation'],
'icon-rotation-alignment': 'map',
'icon-allow-overlap': true
}
});
// Create draggable marker for plane
const planeEl = document.createElement('div');
planeEl.style.width = '40px';
planeEl.style.height = '40px';
planeEl.style.backgroundColor = 'transparent';
planeEl.style.pointerEvents = 'none';
planeMarker = new mapboxgl.Marker({ element: planeEl, draggable: true }).setLngLat(start).addTo(map);
// Drag event handler
planeMarker.on('drag', () => {
const lngLat = planeMarker.getLngLat();
const snapped = turf.nearestPointOnLine(arcLine, [lngLat.lng, lngLat.lat]);
animationIndex = Math.round(snapped.properties.index);
updateFlight(animationIndex);
});
updateFlight(0);
// Slider input
document.getElementById('slider').addEventListener('input', (e) => {
animationIndex = Number(e.target.value);
paused = true;
clearTimeout(animationTimeout);
document.getElementById('pause-button').textContent = "Resume";
updateFlight(animationIndex);
});
// Start button click
document.getElementById('start-button').onclick = () => {
animationIndex = 0;
paused = false;
document.getElementById('pause-button').textContent = "Pause";
animateFlight();
};
// Pause/Resume button click
document.getElementById('pause-button').onclick = () => {
paused = !paused;
document.getElementById('pause-button').textContent = paused ? "Resume" : "Pause";
if (!paused) animateFlight();
else clearTimeout(animationTimeout);
};
});
});
</script>
</body>
</html>