From 508ff9fc728fcc12aea50b8a38101b24b181527e Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 5 Jan 2018 14:53:03 +0100 Subject: [PATCH] nearby: lat & lon -> location object --- docs/nearby.md | 10 +++++++--- index.js | 18 +++++++++++++----- readme.md | 2 +- test/db.js | 6 +++++- test/vbb.js | 6 +++++- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/docs/nearby.md b/docs/nearby.md index 1a646367..3424e281 100644 --- a/docs/nearby.md +++ b/docs/nearby.md @@ -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. -`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: @@ -24,7 +24,11 @@ const vbbProfile = require('hafas-client/p/vbb') 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) .catch(console.error) ``` diff --git a/index.js b/index.js index a02fa68e..5c5f3a6a 100644 --- a/index.js +++ b/index.js @@ -137,9 +137,17 @@ const createClient = (profile) => { }) } - const nearby = (latitude, longitude, opt = {}) => { - if ('number' !== typeof latitude) throw new Error('latitude must be a number.') - if ('number' !== typeof longitude) throw new Error('longitude must be a number.') + const nearby = (location, opt = {}) => { + if ('object' !== typeof location || Array.isArray(location)) { + 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({ results: 8, // maximum number of results distance: null, // maximum walking distance in meters @@ -153,8 +161,8 @@ const createClient = (profile) => { req: { ring: { cCrd: { - x: profile.formatCoord(longitude), - y: profile.formatCoord(latitude) + x: profile.formatCoord(location.longitude), + y: profile.formatCoord(location.latitude) }, maxDist: opt.distance || -1, minDist: 0 diff --git a/readme.md b/readme.md index 23840a56..e957243a 100644 --- a/readme.md +++ b/readme.md @@ -31,7 +31,7 @@ npm install hafas-client - [`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 - [`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 diff --git a/test/db.js b/test/db.js index 735a9c0f..9f9801d6 100644 --- a/test/db.js +++ b/test/db.js @@ -243,7 +243,11 @@ test('departures with station object', 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 }) diff --git a/test/vbb.js b/test/vbb.js index 156a7580..71371a57 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -288,7 +288,11 @@ test('departures at 7-digit station', co.wrap(function* (t) { test('nearby', co.wrap(function* (t) { // 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)) for (let n of nearby) {