journeyLeg: polyline option

This commit is contained in:
Jannis R 2018-04-30 13:12:05 +02:00 committed by Jannis Redmann
parent 7c6e87afff
commit 37770654e1
3 changed files with 17 additions and 4 deletions

View file

@ -25,7 +25,8 @@ With `opt`, you can override the default options, which look like this:
```js ```js
{ {
when: new Date(), 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: [ /* … */ ] 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.

View file

@ -270,7 +270,8 @@ const createClient = (profile, request = _request) => {
throw new Error('lineName must be a non-empty string.') throw new Error('lineName must be a non-empty string.')
} }
opt = Object.assign({ opt = Object.assign({
passedStations: true // return stations on the way? passedStations: true, // return stations on the way?
polyline: false
}, opt) }, opt)
opt.when = opt.when || new Date() opt.when = opt.when || new Date()
@ -280,11 +281,16 @@ const createClient = (profile, request = _request) => {
req: { req: {
jid: ref, jid: ref,
name: lineName, name: lineName,
date: profile.formatDate(profile, opt.when) date: profile.formatDate(profile, opt.when),
getPolyline: !!opt.polyline
} }
}) })
.then((d) => { .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 const leg = { // pretend the leg is contained in a journey
type: 'JNY', type: 'JNY',

View file

@ -13,6 +13,10 @@ client.journeys('900000003201', '900000024101', {results: 1, polylines: true})
// client.nearby(52.5137344, 13.4744798, {distance: 60}) // client.nearby(52.5137344, 13.4744798, {distance: 60})
// client.radar(52.52411, 13.41002, 52.51942, 13.41709, {results: 10}) // 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) => { .then((data) => {
console.log(require('util').inspect(data, {depth: null})) console.log(require('util').inspect(data, {depth: null}))
}) })