-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
552 lines (447 loc) · 15.1 KB
/
index.html
File metadata and controls
552 lines (447 loc) · 15.1 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KML Section Viewer</title>
<style>
:root {
--panel-bg: rgba(255, 255, 255, 0.9);
--panel-border: rgba(15, 23, 42, 0.08);
--panel-text: #122033;
--panel-muted: #4a6078;
--button-bg: #1769e0;
--button-bg-hover: #0f54bb;
--button-text: #ffffff;
--error: #b42318;
--shadow: 0 18px 40px rgba(15, 23, 42, 0.18);
--radius: 16px;
}
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
background: #dfe7ef;
color: var(--panel-text);
}
body {
overflow: hidden;
}
#map {
width: 100%;
height: 100%;
}
.overlay {
position: fixed;
top: 16px;
left: 16px;
z-index: 10;
width: min(360px, calc(100vw - 32px));
padding: 16px 18px;
border: 1px solid var(--panel-border);
border-radius: var(--radius);
background: var(--panel-bg);
box-shadow: var(--shadow);
backdrop-filter: blur(10px);
}
.overlay h1 {
margin: 0;
font-size: 1.15rem;
line-height: 1.3;
}
.overlay p {
margin: 8px 0 0;
font-size: 0.95rem;
line-height: 1.45;
color: var(--panel-muted);
}
.overlay p.error {
color: var(--error);
font-weight: 600;
}
.view-toggle {
display: inline-flex;
gap: 6px;
margin-top: 14px;
padding: 6px;
border: 1px solid var(--panel-border);
border-radius: 999px;
background: rgba(255, 255, 255, 0.82);
}
.view-toggle button {
border: 0;
border-radius: 999px;
padding: 8px 14px;
font: inherit;
font-size: 0.9rem;
font-weight: 600;
color: var(--panel-muted);
background: transparent;
cursor: pointer;
transition: background-color 160ms ease, color 160ms ease;
}
.view-toggle button:hover {
background: rgba(23, 105, 224, 0.1);
color: var(--panel-text);
}
.view-toggle button.active {
background: var(--button-bg);
color: var(--button-text);
}
@media (max-width: 640px) {
.overlay {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.overlay h1 {
font-size: 1.05rem;
}
.overlay p {
font-size: 0.9rem;
}
}
</style>
</head>
<body>
<div class="overlay">
<h1 id="section-title">Section Viewer</h1>
<p id="section-status">Loading map...</p>
<div class="view-toggle" role="group" aria-label="Map view selector">
<button id="roadmap-button" type="button" class="active" aria-pressed="true">Map</button>
<button id="hybrid-button" type="button" aria-pressed="false">Hybrid</button>
</div>
</div>
<div id="map" aria-label="Google Map"></div>
<script>
window.va = window.va || function() {
(window.vaq = window.vaq || []).push(arguments);
};
</script>
<script defer src="/_vercel/insights/script.js"></script>
<script>
const FALLBACK_CENTER = { lat: 5.6037, lng: -0.1870 };
const FALLBACK_ZOOM = 11;
const CONFIG_ENDPOINT = "/api/config";
let map;
let activePolygon = null;
let runtimeConfig = null;
let activeMapTypeId = "roadmap";
async function bootstrapApp() {
try {
runtimeConfig = await fetchRuntimeConfig();
await loadGoogleMapsScript(runtimeConfig.googleMapsApiKey);
initMap();
} catch (error) {
console.error("Application bootstrap failed:", error);
setHeaderState("Section not found", "Configuration could not be loaded.", true);
}
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: FALLBACK_CENTER,
zoom: FALLBACK_ZOOM,
mapTypeId: "roadmap",
streetViewControl: false,
fullscreenControl: true,
mapTypeControl: false
});
bindMapTypeToggle();
updateMapTypeToggleState(activeMapTypeId);
loadSectionFromUrl().catch(function(error) {
console.error("Unhandled map initialization error:", error);
setHeaderState("Section not found", "Unable to load the requested section.", true);
});
}
async function loadSectionFromUrl() {
const params = new URLSearchParams(window.location.search);
const requestedZone = (params.get("zone") || "").trim();
const requestedSection = (params.get("section") || "").trim();
if (!requestedZone || !requestedSection) {
console.warn("Missing required 'zone' or 'section' query parameter.");
setHeaderState("Section not found", "Use a URL like ?zone=Zone%2013§ion=1C.", true);
return;
}
const selectionLabel = buildSelectionLabel(requestedZone, requestedSection);
setHeaderState("Section: " + selectionLabel, "Loading polygon from KML...");
const xmlDoc = await fetchKmlDocument(runtimeConfig.kmlUrl);
const matchResult = resolvePlacemarkForZoneAndSection(xmlDoc, requestedZone, requestedSection);
if (!matchResult) {
console.warn("No matching Placemark was found for zone and section:", requestedZone, requestedSection);
setHeaderState("Section not found", "No exact section match was found inside the requested zone.", true);
return;
}
const matchedName = buildSelectionLabel(matchResult.zoneName, matchResult.sectionName);
const coordinatesText = extractOuterRingCoordinates(matchResult.placemarkNode);
if (!coordinatesText) {
console.warn("Matching Placemark did not contain polygon coordinates:", matchedName);
setHeaderState("Section not found", "The matching KML entry does not contain polygon coordinates.", true);
return;
}
const path = parseCoordinatesString(coordinatesText);
if (!path.length) {
console.warn("Polygon coordinates could not be parsed for section:", matchedName);
setHeaderState("Section not found", "Coordinates were present, but none could be parsed.", true);
return;
}
drawPolygon(path);
fitMapToPath(path);
setHeaderState("Section: " + matchedName, "Polygon loaded successfully.");
console.info("Loaded section:", matchedName, "with", path.length, "points.");
}
async function fetchRuntimeConfig() {
const response = await fetch(CONFIG_ENDPOINT, {
method: "GET",
headers: {
"Accept": "application/json"
}
});
if (!response.ok) {
throw new Error("Failed to fetch runtime config. HTTP status: " + response.status);
}
const config = await response.json();
if (!config.googleMapsApiKey || !config.kmlUrl) {
throw new Error("Runtime config is missing required values.");
}
return config;
}
function loadGoogleMapsScript(apiKey) {
return new Promise(function(resolve, reject) {
const existingScript = document.querySelector("script[data-google-maps-loader='true']");
if (existingScript) {
resolve();
return;
}
window.__initGoogleMap = function() {
resolve();
};
const script = document.createElement("script");
script.src = "https://maps.googleapis.com/maps/api/js?key=" + encodeURIComponent(apiKey) + "&callback=__initGoogleMap";
script.async = true;
script.defer = true;
script.dataset.googleMapsLoader = "true";
script.onerror = function() {
reject(new Error("Google Maps JavaScript API failed to load."));
};
document.head.appendChild(script);
});
}
async function fetchKmlDocument(url) {
console.info("Fetching KML from:", url);
const response = await fetch(url, {
method: "GET",
headers: {
"Accept": "application/vnd.google-earth.kml+xml, application/xml, text/xml"
}
});
if (!response.ok) {
throw new Error("Failed to fetch KML. HTTP status: " + response.status);
}
const xmlText = await response.text();
const xmlDoc = new DOMParser().parseFromString(xmlText, "application/xml");
const parserError = xmlDoc.getElementsByTagName("parsererror")[0];
if (parserError) {
throw new Error("Failed to parse KML XML.");
}
return xmlDoc;
}
function resolvePlacemarkForZoneAndSection(xmlDoc, zoneName, sectionName) {
const targetZoneName = normalizeName(zoneName);
const targetSectionName = normalizeName(sectionName);
const root = xmlDoc.documentElement;
let exactFolderMatch = null;
const stack = [root];
while (stack.length) {
const node = stack.pop();
if (!node || node.nodeType !== Node.ELEMENT_NODE) {
continue;
}
const localName = node.localName;
if (localName === "Folder") {
const folderName = normalizeName(getNodeName(node));
if (folderName === targetZoneName && !exactFolderMatch) {
exactFolderMatch = node;
}
}
for (let index = node.children.length - 1; index >= 0; index -= 1) {
stack.push(node.children[index]);
}
}
if (!exactFolderMatch) {
return null;
}
const descendantPlacemark = findPlacemarkByName(exactFolderMatch, targetSectionName);
if (descendantPlacemark) {
console.info("Matched zone and section:", getNodeName(exactFolderMatch), getNodeName(descendantPlacemark));
return {
zoneName: getNodeName(exactFolderMatch),
sectionName: getNodeName(descendantPlacemark),
placemarkNode: descendantPlacemark,
matchType: "zone-section"
};
}
return null;
}
function findPlacemarkByName(rootNode, sectionName) {
const stack = [rootNode];
while (stack.length) {
const node = stack.pop();
if (!node || node.nodeType !== Node.ELEMENT_NODE) {
continue;
}
if (
node !== rootNode &&
node.localName === "Placemark" &&
normalizeName(getNodeName(node)) === sectionName &&
hasPolygon(node)
) {
return node;
}
for (let index = node.children.length - 1; index >= 0; index -= 1) {
stack.push(node.children[index]);
}
}
return null;
}
function hasPolygon(node) {
return Boolean(findFirstDescendantByLocalName(node, "Polygon"));
}
function extractOuterRingCoordinates(placemarkNode) {
const polygonNode = findFirstDescendantByLocalName(placemarkNode, "Polygon");
if (!polygonNode) {
return "";
}
const outerBoundary = findFirstDirectChildByLocalName(polygonNode, "outerBoundaryIs");
const searchRoot = outerBoundary || polygonNode;
const linearRing = findFirstDescendantByLocalName(searchRoot, "LinearRing");
if (!linearRing) {
return "";
}
const coordinatesNode = findFirstDescendantByLocalName(linearRing, "coordinates");
return coordinatesNode ? coordinatesNode.textContent.trim() : "";
}
function parseCoordinatesString(coordinatesText) {
return coordinatesText
.trim()
.split(/\s+/)
.map(function(tuple) {
const parts = tuple.split(",");
if (parts.length < 2) {
return null;
}
const lng = Number(parts[0]);
const lat = Number(parts[1]);
if (!Number.isFinite(lat) || !Number.isFinite(lng)) {
return null;
}
return { lat: lat, lng: lng };
})
.filter(Boolean);
}
function drawPolygon(path) {
if (activePolygon) {
activePolygon.setMap(null);
}
activePolygon = new google.maps.Polygon({
paths: path,
strokeColor: "#d62828",
strokeOpacity: 1,
strokeWeight: 2.5,
fillColor: "#d62828",
fillOpacity: 0.24,
clickable: false
});
activePolygon.setMap(map);
}
function bindMapTypeToggle() {
const roadmapButton = document.getElementById("roadmap-button");
const hybridButton = document.getElementById("hybrid-button");
roadmapButton.addEventListener("click", function() {
setMapType("roadmap");
});
hybridButton.addEventListener("click", function() {
setMapType("hybrid");
});
}
function setMapType(mapTypeId) {
if (!map) {
activeMapTypeId = mapTypeId;
updateMapTypeToggleState(mapTypeId);
return;
}
activeMapTypeId = mapTypeId;
map.setMapTypeId(mapTypeId);
updateMapTypeToggleState(mapTypeId);
}
function updateMapTypeToggleState(mapTypeId) {
const roadmapButton = document.getElementById("roadmap-button");
const hybridButton = document.getElementById("hybrid-button");
const isHybrid = mapTypeId === "hybrid";
roadmapButton.classList.toggle("active", !isHybrid);
roadmapButton.setAttribute("aria-pressed", String(!isHybrid));
hybridButton.classList.toggle("active", isHybrid);
hybridButton.setAttribute("aria-pressed", String(isHybrid));
}
function fitMapToPath(path) {
if (!path.length) {
map.setCenter(FALLBACK_CENTER);
map.setZoom(FALLBACK_ZOOM);
return;
}
const bounds = new google.maps.LatLngBounds();
path.forEach(function(point) {
bounds.extend(point);
});
map.fitBounds(bounds);
}
function buildSelectionLabel(zoneName, sectionName) {
return normalizeName(zoneName) + " / " + normalizeName(sectionName);
}
function setHeaderState(titleText, statusText, isError) {
const title = document.getElementById("section-title");
const status = document.getElementById("section-status");
title.textContent = titleText;
status.textContent = statusText;
status.classList.toggle("error", Boolean(isError));
}
function getNodeName(node) {
const nameNode = findFirstDirectChildByLocalName(node, "name");
return nameNode ? nameNode.textContent.trim() : "";
}
function normalizeName(value) {
return String(value || "").trim();
}
function findFirstDirectChildByLocalName(node, name) {
for (let index = 0; index < node.children.length; index += 1) {
const child = node.children[index];
if (child.localName === name) {
return child;
}
}
return null;
}
function findFirstDescendantByLocalName(node, name) {
const stack = [node];
while (stack.length) {
const current = stack.pop();
if (!current || current.nodeType !== Node.ELEMENT_NODE) {
continue;
}
if (current !== node && current.localName === name) {
return current;
}
for (let index = current.children.length - 1; index >= 0; index -= 1) {
stack.push(current.children[index]);
}
}
return null;
}
document.addEventListener("DOMContentLoaded", bootstrapApp);
</script>
</body>
</html>