nearby: lat & lon -> location object

This commit is contained in:
Jannis R 2018-01-05 14:53:03 +01:00
parent c4f67e15d3
commit 508ff9fc72
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 31 additions and 11 deletions

View file

@ -1,8 +1,8 @@
# `nearby(latitude, longitude, [opt])` # `nearby(location, [opt])`
This method can be used to find stations close to a location. Note that it is not supported by every profile/endpoint. This method can be used to find stations close to a location. Note that it is not supported by every profile/endpoint.
`latitude` and `longitude` must be GPS coordinates like `52.5137344` and `13.4744798`. `location` must be an [*FPTF* `location` object](https://github.com/public-transport/friendly-public-transport-format/blob/1.0.1/spec/readme.md#location-objects).
With `opt`, you can override the default options, which look like this: With `opt`, you can override the default options, which look like this:
@ -24,7 +24,11 @@ const vbbProfile = require('hafas-client/p/vbb')
const client = createClient(vbbProfile) const client = createClient(vbbProfile)
client.nearby(52.5137344, 13.4744798, {distance: 400}) client.nearby({
type: 'location',
latitude: 52.5137344,
longitude: 13.4744798
}, {distance: 400})
.then(console.log) .then(console.log)
.catch(console.error) .catch(console.error)
``` ```

View file

@ -137,9 +137,17 @@ const createClient = (profile) => {
}) })
} }
const nearby = (latitude, longitude, opt = {}) => { const nearby = (location, opt = {}) => {
if ('number' !== typeof latitude) throw new Error('latitude must be a number.') if ('object' !== typeof location || Array.isArray(location)) {
if ('number' !== typeof longitude) throw new Error('longitude must be a number.') throw new Error('location must be an object.')
} else if (location.type !== 'location') {
throw new Error('invalid location object.')
} else if ('number' !== typeof location.latitude) {
throw new Error('location.latitude must be a number.')
} else if ('number' !== typeof location.longitude) {
throw new Error('location.longitude must be a number.')
}
opt = Object.assign({ opt = Object.assign({
results: 8, // maximum number of results results: 8, // maximum number of results
distance: null, // maximum walking distance in meters distance: null, // maximum walking distance in meters
@ -153,8 +161,8 @@ const createClient = (profile) => {
req: { req: {
ring: { ring: {
cCrd: { cCrd: {
x: profile.formatCoord(longitude), x: profile.formatCoord(location.longitude),
y: profile.formatCoord(latitude) y: profile.formatCoord(location.latitude)
}, },
maxDist: opt.distance || -1, maxDist: opt.distance || -1,
minDist: 0 minDist: 0

View file

@ -31,7 +31,7 @@ npm install hafas-client
- [`journeyLeg(ref, name, [opt])`](docs/journey-leg.md) get details for a leg of a journey - [`journeyLeg(ref, name, [opt])`](docs/journey-leg.md) get details for a leg of a journey
- [`departures(station, [opt])`](docs/departures.md) query the next departures at a station - [`departures(station, [opt])`](docs/departures.md) query the next departures at a station
- [`locations(query, [opt])`](docs/locations.md) find stations, POIs and addresses - [`locations(query, [opt])`](docs/locations.md) find stations, POIs and addresses
- [`nearby(latitude, longitude, [opt])`](docs/nearby.md) show stations & POIs around - [`nearby(location, [opt])`](docs/nearby.md) show stations & POIs around
- [`radar(query, [opt])`](docs/radar.md) find all vehicles currently in a certain area - [`radar(query, [opt])`](docs/radar.md) find all vehicles currently in a certain area

View file

@ -243,7 +243,11 @@ test('departures with station object', co.wrap(function* (t) {
})) }))
test('nearby Berlin Jungfernheide', co.wrap(function* (t) { test('nearby Berlin Jungfernheide', co.wrap(function* (t) {
const nearby = yield client.nearby(52.530273, 13.299433, { const nearby = yield client.nearby({
type: 'location',
latitude: 52.530273,
longitude: 13.299433
}, {
results: 2, distance: 400 results: 2, distance: 400
}) })

View file

@ -288,7 +288,11 @@ test('departures at 7-digit station', co.wrap(function* (t) {
test('nearby', co.wrap(function* (t) { test('nearby', co.wrap(function* (t) {
// Berliner Str./Bundesallee // Berliner Str./Bundesallee
const nearby = yield client.nearby(52.4873452, 13.3310411, {distance: 200}) const nearby = yield client.nearby({
type: 'location',
latitude: 52.4873452,
longitude: 13.3310411
}, {distance: 200})
t.ok(Array.isArray(nearby)) t.ok(Array.isArray(nearby))
for (let n of nearby) { for (let n of nearby) {