Skip to content

Commit e76955f

Browse files
authored
Merge pull request #138 from mapbox/custom-profiles
Custom profiles
2 parents 57ce6e6 + 51aeb65 commit e76955f

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

src/actions/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ function fetchDirections() {
6161
if (alternatives) options.push('alternatives=true');
6262
options.push('steps=true');
6363
options.push('overview=full');
64-
options.push('access_token=' + accessToken);
64+
if (accessToken) options.push('access_token=' + accessToken);
6565
request.abort();
66-
request.open('GET', `${api}mapbox/${profile}/${query}.json?${options.join('&')}`, true);
66+
request.open('GET', `${api}${profile}/${query}.json?${options.join('&')}`, true);
6767

6868
request.onload = () => {
6969
if (request.status >= 200 && request.status < 400) {

src/controls/inputs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default class Inputs {
8383

8484
var destinationEl = this.destinationInput.onAdd();
8585
this.container.querySelector('#mapbox-directions-destination-input').appendChild(destinationEl);
86-
86+
8787

8888
this.originInput.on('result', (e) => {
8989
const coords = e.result.center;
@@ -105,7 +105,7 @@ export default class Inputs {
105105
const profiles = this.container.querySelectorAll('input[type="radio"]');
106106
Array.prototype.forEach.call(profiles, (el) => {
107107
el.addEventListener('change', () => {
108-
setProfile(el.id.replace('mapbox-directions-profile-', ''));
108+
setProfile(el.value);
109109
});
110110
});
111111

src/controls/instructions.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class Instructions {
2828
render() {
2929
this.store.subscribe(() => {
3030
const { hoverMarker, setRouteIndex } = this.actions;
31-
const { routeIndex, unit, directions, error } = this.store.getState();
31+
const { routeIndex, unit, directions, error, compile } = this.store.getState();
3232
const shouldRender = !isEqual(directions[routeIndex], this.directions);
3333

3434
if (error) {
@@ -38,6 +38,15 @@ export default class Instructions {
3838

3939
if (directions.length && shouldRender) {
4040
const direction = this.directions = directions[routeIndex];
41+
42+
if (compile) {
43+
direction.legs.forEach(function(leg) {
44+
leg.steps.forEach(function(step) {
45+
step.maneuver.instruction = compile('en', step);
46+
});
47+
});
48+
}
49+
4150
this.container.innerHTML = instructionsTemplate({
4251
routeIndex,
4352
routes: directions.length,

src/directions.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import Instructions from './controls/instructions';
2323
* @param {Array} [options.styles] Override default layer properties of the [directions source](https://github.com/mapbox/mapbox-gl-directions/blob/master/src/directions_style.js). Documentation for each property are specified in the [Mapbox GL Style Reference](https://www.mapbox.com/mapbox-gl-style-spec/).
2424
* @param {String} [options.accessToken=null] Required unless `mapboxgl.accessToken` is set globally
2525
* @param {Boolean} [options.interactive=true] Enable/Disable mouse or touch interactivity from the plugin
26-
* @param {String} [options.profile="driving-traffic"] Routing profile to use. Options: `driving-traffic`, `driving`, `walking`, `cycling`
26+
* @param {String} [options.profile="mapbox/driving-traffic"] Routing profile to use. Options: `mapbox/driving-traffic`, `mapbox/driving`, `mapbox/walking`, `mapbox/cycling`
2727
* @param {Boolean} [options.alternatives=true] Whether to enable alternatives.
2828
* @param {String} [options.unit="imperial"] Measurement system to be used in navigation instructions. Options: `imperial`, `metric`
29+
* @param {Function} [options.compile=null] Provide a custom function for generating instruction, compatible with osrm-text-instructions.
2930
* @param {Object} [options.geocoder] Pass options available to mapbox-gl-geocoder as [documented here](https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#mapboxglgeocoder).
3031
* @param {Object} [options.controls]
3132
* @param {Boolean} [options.controls.inputs=true] Hide or display the inputs control.
@@ -35,7 +36,7 @@ import Instructions from './controls/instructions';
3536
* var directions = new MapboxDirections({
3637
* accessToken: 'YOUR-MAPBOX-ACCESS-TOKEN',
3738
* unit: 'metric',
38-
* profile: 'cycling'
39+
* profile: 'mapbox/cycling'
3940
* });
4041
* // add to your mapboxgl map
4142
* map.addControl(directions);
@@ -110,7 +111,7 @@ export default class MapboxDirections {
110111
}
111112

112113
mapState() {
113-
const { profile, alternatives, styles, interactive } = store.getState();
114+
const { profile, alternatives, styles, interactive, compile } = store.getState();
114115

115116
// Emit any default or option set config
116117
this.actions.eventEmit('profile', { profile });

src/reducers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import deepAssign from 'deep-assign';
44
const initialState = {
55
// Options set on initialization
66
api: 'https://api.mapbox.com/directions/v5/',
7-
profile: 'driving-traffic',
7+
profile: 'mapbox/driving-traffic',
88
alternatives: false,
99
unit: 'imperial',
10+
compile: null,
1011
proximity: false,
1112
styles: [],
1213

src/templates/inputs.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,32 @@
2424
id='mapbox-directions-profile-driving-traffic'
2525
type='radio'
2626
name='profile'
27-
<% if (profile === 'driving-traffic') { %>checked<% } %>
27+
value='mapbox/driving-traffic'
28+
<% if (profile === 'mapbox/driving-traffic') { %>checked<% } %>
2829
/>
2930
<label for='mapbox-directions-profile-driving-traffic'>Traffic</label>
3031
<input
3132
id='mapbox-directions-profile-driving'
3233
type='radio'
3334
name='profile'
34-
<% if (profile === 'driving') { %>checked<% } %>
35+
value='mapbox/driving'
36+
<% if (profile === 'mapbox/driving') { %>checked<% } %>
3537
/>
3638
<label for='mapbox-directions-profile-driving'>Driving</label>
3739
<input
3840
id='mapbox-directions-profile-walking'
3941
type='radio'
4042
name='profile'
41-
<% if (profile === 'walking') { %>checked<% } %>
43+
value='mapbox/walking'
44+
<% if (profile === 'mapbox/walking') { %>checked<% } %>
4245
/>
4346
<label for='mapbox-directions-profile-walking'>Walking</label>
4447
<input
4548
id='mapbox-directions-profile-cycling'
4649
type='radio'
4750
name='profile'
48-
<% if (profile === 'cycling') { %>checked<% } %>
51+
value='mapbox/cycling'
52+
<% if (profile === 'mapbox/cycling') { %>checked<% } %>
4953
/>
5054
<label for='mapbox-directions-profile-cycling'>Cycling</label>
5155
</div>

0 commit comments

Comments
 (0)