diff --git a/themes/intotheeast/templates/trip.html.twig b/themes/intotheeast/templates/trip.html.twig index 8f12790..126560a 100644 --- a/themes/intotheeast/templates/trip.html.twig +++ b/themes/intotheeast/templates/trip.html.twig @@ -94,7 +94,8 @@ 'lng': item.page.header.lng|number_format(6, '.', ''), 'slug': item.page.slug, 'title': item.page.title, - 'url': item.page.url + 'url': item.page.url, + 'force_connect': item.page.header.force_connect ? true : false }]) %} {% endif %} {% endfor %} @@ -301,35 +302,17 @@ var tripMap = new maplibregl.Map({ }); tripMap.on('load', function () { - GPX_URLS.forEach(function (url, idx) { - fetch(url) - .then(function (r) { return r.text(); }) - .then(function (text) { - var xml = new DOMParser().parseFromString(text, 'text/xml'); - var geojson = toGeoJSON.gpx(xml); - var sid = 'gpx-' + idx; - tripMap.addSource(sid, { type: 'geojson', data: geojson }); - tripMap.addLayer({ - id: sid + '-line', type: 'line', source: sid, - layout: { 'line-join': 'round', 'line-cap': 'round' }, - paint: { 'line-color': MapUtils.ACCENT, 'line-width': 2, 'line-opacity': 0.7 } - }); - }) - .catch(function (err) { console.warn('GPX load failed:', url, err); }); - }); - if (TRIP_ENTRIES.length === 0) { tripMap.jumpTo({ center: [0, 20], zoom: 2 }); return; } + /* ── Markers + bounds ──────────────────────────────────────── */ var bounds = new maplibregl.LngLatBounds(); - var coords = []; TRIP_ENTRIES.forEach(function (entry, i) { var isLatest = (i === TRIP_ENTRIES.length - 1); var lngLat = [parseFloat(entry.lng), parseFloat(entry.lat)]; - coords.push(lngLat); bounds.extend(lngLat); var el = MapUtils.createDotMarker(isLatest); @@ -347,13 +330,38 @@ tripMap.on('load', function () { new maplibregl.Marker({ element: el }).setLngLat(lngLat).addTo(tripMap); }); - MapUtils.addJourneyLine(tripMap, coords, 'trip-journey'); - + /* ── Fit bounds ─────────────────────────────────────────────── */ if (TRIP_ENTRIES.length === 1) { - tripMap.jumpTo({ center: coords[0], zoom: 10 }); + tripMap.jumpTo({ center: [parseFloat(TRIP_ENTRIES[0].lng), parseFloat(TRIP_ENTRIES[0].lat)], zoom: 10 }); } else { tripMap.fitBounds(bounds, { padding: 60, maxZoom: 11 }); } + + /* ── GPX tracks + journey segments ─────────────────────────── */ + Promise.all(GPX_URLS.map(function (url, idx) { + return fetch(url) + .then(function (r) { return r.text(); }) + .then(function (text) { + var xml = new DOMParser().parseFromString(text, 'text/xml'); + var geojson = toGeoJSON.gpx(xml); + var sid = 'gpx-' + idx; + tripMap.addSource(sid, { type: 'geojson', data: geojson }); + tripMap.addLayer({ + id: sid + '-line', type: 'line', source: sid, + layout: { 'line-join': 'round', 'line-cap': 'round' }, + paint: { 'line-color': MapUtils.ACCENT, 'line-width': 2, 'line-opacity': 0.7 } + }); + return MapUtils.extractTrackpoints(geojson); + }) + .catch(function (err) { + console.warn('GPX load failed:', url, err); + return []; + }); + })).then(function (allTrackpoints) { + var validTrackpoints = allTrackpoints.filter(function (tp) { return tp.length > 0; }); + var segments = MapUtils.buildJourneySegments(TRIP_ENTRIES, validTrackpoints, 10); + MapUtils.addJourneySegments(tripMap, segments, 'trip-journey'); + }); }); setTimeout(function () { tripMap.resize(); }, 100);