feat: use buildJourneySegments in trip.html.twig mini-map
This commit is contained in:
@@ -94,7 +94,8 @@
|
|||||||
'lng': item.page.header.lng|number_format(6, '.', ''),
|
'lng': item.page.header.lng|number_format(6, '.', ''),
|
||||||
'slug': item.page.slug,
|
'slug': item.page.slug,
|
||||||
'title': item.page.title,
|
'title': item.page.title,
|
||||||
'url': item.page.url
|
'url': item.page.url,
|
||||||
|
'force_connect': item.page.header.force_connect ? true : false
|
||||||
}]) %}
|
}]) %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@@ -301,35 +302,17 @@ var tripMap = new maplibregl.Map({
|
|||||||
});
|
});
|
||||||
|
|
||||||
tripMap.on('load', function () {
|
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) {
|
if (TRIP_ENTRIES.length === 0) {
|
||||||
tripMap.jumpTo({ center: [0, 20], zoom: 2 });
|
tripMap.jumpTo({ center: [0, 20], zoom: 2 });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ── Markers + bounds ──────────────────────────────────────── */
|
||||||
var bounds = new maplibregl.LngLatBounds();
|
var bounds = new maplibregl.LngLatBounds();
|
||||||
var coords = [];
|
|
||||||
|
|
||||||
TRIP_ENTRIES.forEach(function (entry, i) {
|
TRIP_ENTRIES.forEach(function (entry, i) {
|
||||||
var isLatest = (i === TRIP_ENTRIES.length - 1);
|
var isLatest = (i === TRIP_ENTRIES.length - 1);
|
||||||
var lngLat = [parseFloat(entry.lng), parseFloat(entry.lat)];
|
var lngLat = [parseFloat(entry.lng), parseFloat(entry.lat)];
|
||||||
coords.push(lngLat);
|
|
||||||
bounds.extend(lngLat);
|
bounds.extend(lngLat);
|
||||||
|
|
||||||
var el = MapUtils.createDotMarker(isLatest);
|
var el = MapUtils.createDotMarker(isLatest);
|
||||||
@@ -347,13 +330,38 @@ tripMap.on('load', function () {
|
|||||||
new maplibregl.Marker({ element: el }).setLngLat(lngLat).addTo(tripMap);
|
new maplibregl.Marker({ element: el }).setLngLat(lngLat).addTo(tripMap);
|
||||||
});
|
});
|
||||||
|
|
||||||
MapUtils.addJourneyLine(tripMap, coords, 'trip-journey');
|
/* ── Fit bounds ─────────────────────────────────────────────── */
|
||||||
|
|
||||||
if (TRIP_ENTRIES.length === 1) {
|
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 {
|
} else {
|
||||||
tripMap.fitBounds(bounds, { padding: 60, maxZoom: 11 });
|
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);
|
setTimeout(function () { tripMap.resize(); }, 100);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user