From a005c32cf748fb6980bdeac2907b7824a2c03034 Mon Sep 17 00:00:00 2001 From: Freenerd Date: Thu, 2 Jul 2015 15:40:02 +0200 Subject: [PATCH] Configurable directions/geocode hosts --- API.md | 9 ++++++--- src/directions.js | 46 +++++++++++++++++++++++++++++----------------- test/directions.js | 32 +++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/API.md b/API.md index 5b82873..aa57a4b 100644 --- a/API.md +++ b/API.md @@ -4,9 +4,12 @@ _Extends_: `L.Class` -| Options | Value | Description | -| ---- | ---- | ---- | -| options | object | `accessToken` is a required property unless `L.mapbox.accessToken` is set globally. `profile` is optional and defaults to `mapbox.driving`. | +| Option | Description | +| ---- | ---- | +| accessToken | Required, unless `L.mapbox.accessToken` is set globally +| profile | Optional, defaults to `mapbox.driving`, other options `mapbox.walking` and `mapbox.cycling` +| directionsHost | Optional, overrides directions host, defaults to Mapbox Directions API +| geocodeHost | Optional, overrives geocode host, defaults to Mapbox Geocode API ### directions.getOrigin() diff --git a/src/directions.js b/src/directions.js index 65fc5ce..175d41b 100644 --- a/src/directions.js +++ b/src/directions.js @@ -8,12 +8,14 @@ var Directions = L.Class.extend({ includes: [L.Mixin.Events], options: { - units: 'imperial' + units: 'imperial', + geocodeHost: 'https://api.tiles.mapbox.com', + directionsHost: 'https://api.tiles.mapbox.com' }, statics: { - URL_TEMPLATE: 'https://api.tiles.mapbox.com/v4/directions/{profile}/{waypoints}.json?instructions=html&geometry=polyline&access_token={token}', - GEOCODER_TEMPLATE: 'https://api.tiles.mapbox.com/v4/geocode/mapbox.places/{query}.json?proximity={proximity}&access_token={token}' + URL_TEMPLATE: '{host}/v4/directions/{profile}/{waypoints}.json?instructions=html&geometry=polyline&access_token={token}', + GEOCODER_TEMPLATE: '{host}/v4/geocode/mapbox.places/{query}.json?proximity={proximity}&access_token={token}' }, initialize: function(options) { @@ -117,6 +119,7 @@ var Directions = L.Class.extend({ queryURL: function () { var template = Directions.URL_TEMPLATE, + host = this.options.directionsHost, token = this.options.accessToken || L.mapbox.accessToken, profile = this.getProfile(), points = [this.origin].concat(this._waypoints).concat([this.destination]).map(function (point) { @@ -128,6 +131,7 @@ var Directions = L.Class.extend({ } return L.Util.template(template, { + host: host, token: token, profile: profile, waypoints: points @@ -199,26 +203,34 @@ var Directions = L.Class.extend({ return this; }, - _geocode: function(waypoint, proximity, cb) { - if (!this._requests) this._requests = []; - this._requests.push(request(L.Util.template(Directions.GEOCODER_TEMPLATE, { + geocodeURL: function(waypoint, proximity) { + return L.Util.template(Directions.GEOCODER_TEMPLATE, { + host: this.options.geocodeHost, query: waypoint.properties.query, token: this.options.accessToken || L.mapbox.accessToken, proximity: proximity ? [proximity.lng, proximity.lat].join(',') : '' - }), L.bind(function (err, resp) { - if (err) { - return cb(err); - } + }); + }, - if (!resp.features || !resp.features.length) { - return cb(new Error("No results found for query " + waypoint.properties.query)); - } + _geocode: function(waypoint, proximity, cb) { + if (!this._requests) this._requests = []; + this._requests.push(request( + this.geocodeURL(waypoint, proximity), + L.bind(function (err, resp) { + if (err) { + return cb(err); + } + + if (!resp.features || !resp.features.length) { + return cb(new Error("No results found for query " + waypoint.properties.query)); + } - waypoint.geometry.coordinates = resp.features[0].center; - waypoint.properties.name = resp.features[0].place_name; + waypoint.geometry.coordinates = resp.features[0].center; + waypoint.properties.name = resp.features[0].place_name; - return cb(); - }, this))); + return cb(); + }, this) + )); }, _unload: function () { diff --git a/test/directions.js b/test/directions.js index 3e55d67..c6cf914 100644 --- a/test/directions.js +++ b/test/directions.js @@ -147,12 +147,21 @@ describe("Directions", function () { }); describe("queryURL", function () { - it("constructs a URL with origin and destination", function () { + it("constructs a URL with origin and destination for default host", function () { var directions = L.mapbox.directions({accessToken: 'key'}); directions.setOrigin(L.latLng(1, 2)).setDestination(L.latLng(3, 4)); expect(directions.queryURL()).to.eql('https://api.tiles.mapbox.com/v4/directions/mapbox.driving/2,1;4,3.json?instructions=html&geometry=polyline&access_token=key'); }); + it("constructs a URL with different host", function () { + var directions = L.mapbox.directions({ + accessToken: 'key', + directionsHost: 'https://directions.example.com' + }); + directions.setOrigin(L.latLng(1, 2)).setDestination(L.latLng(3, 4)); + expect(directions.queryURL()).to.eql('https://directions.example.com/v4/directions/mapbox.driving/2,1;4,3.json?instructions=html&geometry=polyline&access_token=key'); + }); + it("wraps coordinates", function () { var directions = L.mapbox.directions({accessToken: 'key'}); directions.setOrigin(L.latLng(0, 190)).setDestination(L.latLng(0, -195)); @@ -323,6 +332,27 @@ describe("Directions", function () { }); }); + describe("geocodeURL", function () { + it("constructs a URL with correct priximity and waypoint for default host", function () { + var directions = L.mapbox.directions({accessToken: 'key'}); + expect(directions.geocodeURL( + directions._normalizeWaypoint('San Francisco'), + {lat: 2, lng: 2} + )).to.eql('https://api.tiles.mapbox.com/v4/geocode/mapbox.places/San Francisco.json?proximity=2,2&access_token=key'); + }); + + it("constructs a URL with different host", function () { + var directions = L.mapbox.directions({ + accessToken: 'key', + geocodeHost: 'https://geocode.example.com' + }); + expect(directions.geocodeURL( + directions._normalizeWaypoint('San Francisco'), + {lat: 2, lng: 2} + )).to.eql('https://geocode.example.com/v4/geocode/mapbox.places/San Francisco.json?proximity=2,2&access_token=key'); + }); + }); + describe("geocode", function () { var server;