diff --git a/docs/journey-leg.md b/docs/journey-leg.md index 95de8c14..f54b0e6e 100644 --- a/docs/journey-leg.md +++ b/docs/journey-leg.md @@ -25,7 +25,8 @@ With `opt`, you can override the default options, which look like this: ```js { when: new Date(), - passedStations: true // return stations on the way? + passedStations: true, // return stations on the way? + polyline: false // return a shape for the leg? } ``` @@ -116,3 +117,5 @@ The response looked like this: passed: [ /* … */ ] } ``` + +If you pass `polyline: true`, the leg will have a `polyline` field, containing an encoded shape. You can use e.g. [`@mapbox/polyline`](https://www.npmjs.com/package/@mapbox/polyline) to decode it. diff --git a/index.js b/index.js index 93c9fdc9..f6762927 100644 --- a/index.js +++ b/index.js @@ -270,7 +270,8 @@ const createClient = (profile, request = _request) => { throw new Error('lineName must be a non-empty string.') } opt = Object.assign({ - passedStations: true // return stations on the way? + passedStations: true, // return stations on the way? + polyline: false }, opt) opt.when = opt.when || new Date() @@ -280,11 +281,16 @@ const createClient = (profile, request = _request) => { req: { jid: ref, name: lineName, - date: profile.formatDate(profile, opt.when) + date: profile.formatDate(profile, opt.when), + getPolyline: !!opt.polyline } }) .then((d) => { - const parse = profile.parseJourneyLeg(profile, d.locations, d.lines, d.remarks) + let polylines = [] + if (opt.polyline && Array.isArray(d.common.polyL)) { + polylines = d.common.polyL.map(p => p.crdEncYX) + } + const parse = profile.parseJourneyLeg(profile, d.locations, d.lines, d.remarks, polylines) const leg = { // pretend the leg is contained in a journey type: 'JNY', diff --git a/p/vbb/example.js b/p/vbb/example.js index 0f0831c7..85d6ea28 100644 --- a/p/vbb/example.js +++ b/p/vbb/example.js @@ -13,6 +13,10 @@ client.journeys('900000003201', '900000024101', {results: 1, polylines: true}) // client.nearby(52.5137344, 13.4744798, {distance: 60}) // client.radar(52.52411, 13.41002, 52.51942, 13.41709, {results: 10}) +// .then(([journey]) => { +// const leg = journey.legs[0] +// return client.journeyLeg(leg.id, leg.line.name, {polyline: true}) +// }) .then((data) => { console.log(require('util').inspect(data, {depth: null})) })