diff --git a/docs/services.md b/docs/services.md index 148562a..b4259d6 100644 --- a/docs/services.md +++ b/docs/services.md @@ -2183,6 +2183,7 @@ Type: [Object][200] and the second is the range of degrees the angle can deviate by. * `radius` **([number][205] | `"unlimited"`)?** Maximum distance in meters that the coordinate is allowed to move when snapped to a nearby road segment. * `waypointName` **[string][201]?** Custom name for the waypoint used for the arrival instruction in banners and voice instructions. +* `silent` **[boolean][202]?** Used to indicate if the waypoint should be used to split a route into distinct legs. The first and last waypoints can't be set as silent. ### MapMatchingPoint diff --git a/services/__tests__/directions.test.js b/services/__tests__/directions.test.js index 313b95b..cccb863 100644 --- a/services/__tests__/directions.test.js +++ b/services/__tests__/directions.test.js @@ -167,6 +167,34 @@ describe('getDirections', () => { }); }); + test('it works with optional waypoints.silent', () => { + directions.getDirections({ + waypoints: [ + { + coordinates: [2.2, 1.1] + }, + { + coordinates: [2.2, 1.1], + silent: true + }, + { + coordinates: [2.2, 1.1] + } + ] + }); + expect(tu.requestConfig(directions)).toEqual({ + path: '/directions/v5/mapbox/:profile/:coordinates', + method: 'GET', + params: { + coordinates: '2.2,1.1;2.2,1.1;2.2,1.1', + profile: 'driving' + }, + query: { + waypoints: '0;2' + } + }); + }); + test('waypoints.radius can be any of string or number', () => { directions.getDirections({ waypoints: [ @@ -221,4 +249,21 @@ describe('getDirections', () => { }); }).toThrowError(/between 2 and 25/); }); + + test('errors if first or last waypoints are silent', () => { + expect(() => { + directions.getDirections({ + waypoints: [ + { + coordinates: [2.2, 1.1], + silent: true + }, + { + coordinates: [2.2, 1.1], + silent: true + } + ] + }); + }).toThrowError(/first and last waypoints cannot be silent/); + }); }); diff --git a/services/directions.js b/services/directions.js index 797f99f..7b31355 100644 --- a/services/directions.js +++ b/services/directions.js @@ -81,7 +81,8 @@ Directions.getDirections = function(config) { approach: v.oneOf('unrestricted', 'curb'), bearing: v.arrayOf(v.range([0, 360])), radius: v.oneOfType(v.number, v.equal('unlimited')), - waypointName: v.string + waypointName: v.string, + silent: v.boolean }) ) ), @@ -129,7 +130,9 @@ Directions.getDirections = function(config) { approach: [], bearing: [], radius: [], - waypointName: [] + waypointName: [], + waypoints: [], + silent: [] }; var waypointCount = config.waypoints.length; @@ -148,12 +151,27 @@ Directions.getDirections = function(config) { * and the second is the range of degrees the angle can deviate by. * @property {number|'unlimited'} [radius] - Maximum distance in meters that the coordinate is allowed to move when snapped to a nearby road segment. * @property {string} [waypointName] - Custom name for the waypoint used for the arrival instruction in banners and voice instructions. + * @property {boolean} [silent=false] - Used to indicate if the waypoint should be used to split a route into distinct legs. The first and last waypoints can't be set as silent. */ - config.waypoints.forEach(function(waypoint) { + config.waypoints.forEach(function(waypoint, index) { path.coordinates.push( waypoint.coordinates[0] + ',' + waypoint.coordinates[1] ); + if ( + (index === 0 && waypoint.silent) || + (index === config.waypoints.length - 1 && waypoint.silent) + ) { + throw new Error('first and last waypoints cannot be silent'); + } + + if (!waypoint.silent) { + path.waypoints.push(index); + } + if (waypoint.hasOwnProperty('silent')) { + path.silent.push(waypoint.silent); + } + // join props which come in pairs ['bearing'].forEach(function(prop) { if (waypoint.hasOwnProperty(prop) && waypoint[prop] != null) { @@ -183,6 +201,17 @@ Directions.getDirections = function(config) { } }); + if ( + path.silent.every(function(v) { + return !v; + }) + ) { + delete path.waypoints; + } else { + path.waypoints = path.waypoints.join(';'); + } + delete path.silent; + var query = stringifyBooleans({ alternatives: config.alternatives, annotations: config.annotations, @@ -211,7 +240,8 @@ Directions.getDirections = function(config) { ev_max_ac_charging_power: config.ev_max_ac_charging_power, ev_min_charge_at_destination: config.ev_min_charge_at_destination, ev_min_charge_at_charging_station: config.ev_min_charge_at_charging_station, - auxiliary_consumption: config.auxiliary_consumption + auxiliary_consumption: config.auxiliary_consumption, + waypoints: path.waypoints }); return this.client.createRequest({