Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

<span class='leaflet icon'>_Extends_: `L.Class`</span>

| 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()

Expand Down
46 changes: 29 additions & 17 deletions src/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -128,6 +131,7 @@ var Directions = L.Class.extend({
}

return L.Util.template(template, {
host: host,
token: token,
profile: profile,
waypoints: points
Expand Down Expand Up @@ -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 () {
Expand Down
32 changes: 31 additions & 1 deletion test/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;

Expand Down