Files
f1-circuits/examples/map.html
T
jschoubben db9f386c7a Add a library page: seasons, race weekends and sessions as Plex shows them
The operator wants to browse the posters the way a Plex or Emby Formula 1
library shows them. The page reads the whole library from the one Kometa
file (rounds, race names in the race posters' addresses, sessions in the
episodes'), waits through a fresh server's minutes-long first answer
instead of failing, and loads each picture only when it is on screen, two
at a time, dropping the queue when the reader leaves a level: every
poster is a render of about 10 s that pulls map tiles. Each level has its
own address and back/forward work; a picture the server has none for is
a plain card with its name.

The session order per weekend format moves into site.js, so the posters
page and the library read the same list.

Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
2026-11-27 10:33:19 +01:00

187 lines
7.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GPS traces on a map · F1 Circuits API examples</title>
<meta name="description" content="Draw an F1 track layout's GeoJSON GPS trace on a Leaflet map with OpenStreetMap or satellite tiles.">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="">
<link rel="icon" href="data:,">
<link rel="stylesheet" href="site.css">
</head>
<body>
<header class="site">
<a class="brand" href="index.html">F1 <span>Circuits</span> API</a>
<nav aria-label="Examples">
<a href="index.html">Overview</a>
<a href="circuits.html">Circuit browser</a>
<a href="tracks.html">Track images</a>
<a href="map.html" aria-current="page">GPS traces</a>
<a href="grand-prix.html">Grand prix lookup</a>
<a href="posters.html">Posters</a>
<a href="kometa.html">Kometa</a>
<a href="library.html">Library</a>
</nav>
<div id="api-base"></div>
</header>
<main>
<h1>GPS traces on a map</h1>
<p class="lead">
Each layout was traced by GPS along the track. Its <code>geojson</code> format is that trace: a GeoJSON
<code>FeatureCollection</code> holding one <code>LineString</code> of <code>[longitude, latitude]</code> points. Any
map library draws it as it comes; this page uses <a href="https://leafletjs.com/">Leaflet</a>. Switch to the
satellite layer to see the trace follow the tarmac.
</p>
<div class="card" id="tree">
<h3>The picker is filled from one call</h3>
<div class="request"></div>
<div class="status"></div>
<div class="result"></div>
</div>
<section class="card" id="trace">
<h2>A layout’s trace</h2>
<p><code>/circuits/{country}/{city}/{circuit}/layout/{layout}/geojson</code></p>
<div id="raw-note"></div>
<div class="controls" id="picker"></div>
<div class="request"></div>
<div class="status idle">Choose a layout.</div>
<div id="problem"></div>
<div id="map" role="region" aria-label="Map of the circuit"></div>
<div id="facts"></div>
<details><summary>The GeoJSON answer</summary><div class="result"></div></details>
</section>
<h2>Draw it yourself</h2>
<p>The whole of it, with Leaflet loaded:</p>
<pre class="code" id="snippet"></pre>
</main>
<footer class="pager">
<a rel="prev" href="tracks.html">← Track images</a>
<a class="home" href="index.html">All examples</a>
<a rel="next" href="grand-prix.html">Grand prix lookup →</a>
</footer>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<script src="site.js"></script>
<script>
(function () {
const { h, api, path, call, layoutPicker, layoutYears, showJSON, notice, lineOf, lineLength } = F1;
const $ = (id) => document.getElementById(id);
const card = $('trace');
// ?raw=<base> reads the repository's own files (circuits/<country>/<city>/<circuit>/<layout>.geo.json)
// from <base> instead of the API: a way to try the page against the traces themselves.
const raw = (F1.params.get('raw') || '').replace(/\/+$/, '');
if (raw) {
$('raw-note').append(notice('info', 'Reading the repository’s raw trace files from ', h('code', {}, F1.absolute(raw)),
' (set by ?raw=), not from the API. Remove ?raw= from the address to call the API.'));
}
const map = L.map('map', { scrollWheelZoom: false }).setView([50.44, 5.97], 14);
const osm = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
const satellite = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
maxZoom: 19,
maxNativeZoom: 18,
attribution: 'Imagery &copy; Esri, Maxar, Earthstar Geographics',
});
L.control.layers({ 'OpenStreetMap': osm, 'Satellite (Esri)': satellite }).addTo(map);
L.control.scale({ imperial: false }).addTo(map);
map.on('focus', () => map.scrollWheelZoom.enable());
map.on('blur', () => map.scrollWheelZoom.disable());
let drawn = null;
function traceURL(p) {
if (raw) {
const e = encodeURIComponent;
return `${raw}/${e(p.country)}/${e(p.city)}/${e(p.circuit)}/${e(p.layout)}.geo.json`;
}
return api(path`/circuits/${p.country}/${p.city}/${p.circuit}/layout/${p.layout}/geojson`);
}
function facts(p, data) {
const line = lineOf(data);
const props = line.properties || {};
const coords = line.geometry.coordinates;
const traced = Math.round(lineLength(coords));
const rows = [
['Circuit', `${p.circuitName}, ${p.cityName} (${p.countryName})`],
['Layout', `${layoutYears(p.layout)} (${p.layout})`],
['GPS points', String(coords.length)],
['Length of the trace', `${(traced / 1000).toFixed(3)} km, measured along the points on this page`],
];
if (props.length) rows.push(['Length (property)', `${(props.length / 1000).toFixed(3)} km`]);
if (props.altitude !== undefined) rows.push(['Altitude (property)', `${props.altitude} m`]);
for (const [k, v] of Object.entries(props)) {
if (['length', 'altitude'].includes(k) || typeof v === 'object') continue;
rows.push([`${k} (property)`, String(v)]);
}
$('facts').replaceChildren(h('table', { class: 'kv' },
...rows.map(([k, v]) => h('tr', {}, h('th', {}, k), h('td', {}, v)))));
}
async function draw(p) {
if (!p.layout) return;
const url = traceURL(p);
const answer = await call(card, url, { label: raw ? 'raw file' : null, waiting: 'Fetching the trace…' });
const out = card.querySelector('.result');
if (drawn) { drawn.remove(); drawn = null; }
$('facts').replaceChildren();
$('problem').replaceChildren();
out.replaceChildren();
if (!answer) return;
const fail = (why, ...content) => {
const status = card.querySelector('.status');
F1.setStatus(status, 'error', `${status.textContent} — ${why}`);
$('problem').replaceChildren(notice('error', ...content));
};
const text = await answer.blob.text();
let data;
try {
data = JSON.parse(text);
} catch {
const kind = await F1.sniff(answer.blob);
fail('not GeoJSON',
`Nothing drawn: the answer is ${kind === 'svg' ? 'an SVG document' : 'not JSON'}, not GeoJSON (its Content-Type header says ${answer.type}). It starts with: `,
h('code', {}, text.slice(0, 120)));
return;
}
if (!lineOf(data)) {
fail('no LineString', 'Nothing drawn: the answer is JSON but holds no LineString feature.');
showJSON(out, data);
return;
}
drawn = L.featureGroup().addTo(map);
L.geoJSON(data, { style: { color: '#e10600', weight: 4, opacity: 0.9 } }).addTo(drawn);
const [lon, lat] = lineOf(data).geometry.coordinates[0];
L.circleMarker([lat, lon], { radius: 7, color: '#ffffff', weight: 2, fillColor: '#15151e', fillOpacity: 1 })
.bindTooltip('First point of the trace').addTo(drawn);
map.fitBounds(drawn.getBounds(), { padding: [30, 30] });
facts(p, data);
showJSON(out, data);
}
$('snippet').textContent = [
`const map = L.map('map');`,
`L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {`,
` attribution: '© OpenStreetMap contributors' }).addTo(map);`,
`const res = await fetch('${F1.absolute(api('/circuits/belgium/spa/circuit-de-spa-francorchamps/layout/1983-/geojson'))}');`,
`const track = L.geoJSON(await res.json(), { style: { color: '#e10600', weight: 4 } }).addTo(map);`,
`map.fitBounds(track.getBounds());`,
].join('\n');
layoutPicker($('picker'), { treeCard: $('tree'), sync: true, onChange: draw });
})();
</script>
</body>
</html>