Fix route display: parse track coordinates from GPX on init

The route polyline wasn't visible because segments were initialized
empty. Now extractSegmentsFromGpx() parses trkpt coordinates from
the GPX string via regex on init, so the route shows immediately
in both view and edit modes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-04-13 07:57:04 +02:00
parent bd311cd339
commit d45f1e14fd
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9

View file

@ -21,9 +21,10 @@ export interface EditorState {
export function useRouteEditor(route: RouteDetail) {
const [state, setState] = useState<EditorState>(() => {
const waypoints = extractWaypoints(route);
const segments = extractSegmentsFromGpx(route.gpx);
return {
waypoints,
segments: [],
segments,
dirty: false,
computing: false,
saving: false,
@ -174,6 +175,27 @@ function extractWaypoints(route: RouteDetail): Waypoint[] {
}
}
function extractSegmentsFromGpx(gpx: string | null): RouteSegment[] {
if (!gpx) return [];
try {
const segments: RouteSegment[] = [];
const trkptRegex = /<trkpt\s+lat="([^"]+)"\s+lon="([^"]+)"/g;
const coordinates: [number, number][] = [];
let match;
while ((match = trkptRegex.exec(gpx)) !== null) {
const lat = parseFloat(match[1]!);
const lon = parseFloat(match[2]!);
coordinates.push([lon, lat]); // GeoJSON order: [lon, lat]
}
if (coordinates.length > 0) {
segments.push({ coordinates });
}
return segments;
} catch {
return [];
}
}
function extractCoordsFromGeojson(geojson: unknown): [number, number][] {
try {
const features = (geojson as { features?: unknown[] })?.features;