A reader learns the API fastest from working calls: one page per area (circuit browser, track images, GPS traces on a map, grand prix lookup, posters, Kometa) shows the exact request URL next to its answer. Pages call their own origin when the app serves them, and the public service when opened from disk or given ?api=. Posters render only on a button press, since each one fetches map tiles on the server. A body that does not match the asked format is said in red, never shown as success. The image copies examples/ so the deployed app serves the same pages. Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
186 lines
7.8 KiB
HTML
186 lines
7.8 KiB
HTML
<!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>
|
||
</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: '© <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 © 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>
|