diff --git a/src/routes/api.ts b/src/routes/api.ts index cc03750..edd71b9 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -9,6 +9,7 @@ import * as journeyService from '../services/journey'; import * as reminderService from '../services/reminder'; import * as graphBuilder from '../services/graphBuilder'; import { startBackgroundJobs } from '../jobs'; +import * as walking from '../walking/walkingMap'; /** * Express router for the MBus API v3. @@ -400,6 +401,31 @@ export async function planJourney(req: express.Request, res: express.Response) { } router.get('/plan-journey', planJourney); +export const PlanWalkingQuery = z.object({ + originLat: z.coerce.number(), originLon: z.coerce.number(), + destLat: z.coerce.number(), destLon: z.coerce.number() +}); + +/** Calculates just the new walking path and responds with a [WalkingResponse] */ +export async function planWalking(req: express.Request, res: express.Response) { + try { + const query = PlanWalkingQuery.safeParse(req.query); + if (!query.success) { + res.status(400); + res.send(query.error.message); + return; + } + const { originLat, originLon, destLat, destLon } = query.data; + const walkingResponse = await walking.getWalkingResponse(originLat, originLon, destLat, destLon); + + res.json(walkingResponse); + } catch (error) { + console.error("Journey plan error:", error); + res.status(500).json({ error: 'Journey planning failed' }); + } +} +router.get('/plan-walking', planWalking); + /** * Saves the current graph state to a file (DEV mode only). * @param req - Express request diff --git a/src/walking/walkingMap.ts b/src/walking/walkingMap.ts index b105667..02da523 100644 --- a/src/walking/walkingMap.ts +++ b/src/walking/walkingMap.ts @@ -15,6 +15,14 @@ export interface WalkingResponse { distance: number; /** Ordered list of coordinates representing the walking path geometry. */ path_coords: { lat: number, lon: number }[]; + /** Turn by turn directions. */ + directions: { path_index: number, turn: Turn }[]; +} + +export interface Turn { + /** [-180, 180]ish range, a positive number represents a left turn */ + degrees: number, + landmark: string, } /**