2 // Doesn't yet support hints
4 function OSRMEngine() {
9 creditline: '<a href="http://project-osrm.org/" target="_blank">OSRM</a>',
12 _transformSteps: function(input_steps, line) {
13 var INSTRUCTION_TEMPLATE = {
14 'continue': 'javascripts.directions.instructions.continue',
15 'merge right': 'javascripts.directions.instructions.merge_right',
16 'merge left': 'javascripts.directions.instructions.merge_left',
17 'off ramp right': 'javascripts.directions.instructions.offramp_right',
18 'off ramp left': 'javascripts.directions.instructions.offramp_left',
19 'on ramp right': 'javascripts.directions.instructions.onramp_right',
20 'on ramp left': 'javascripts.directions.instructions.onramp_left',
21 'fork right': 'javascripts.directions.instructions.fork_right',
22 'fork left': 'javascripts.directions.instructions.fork_left',
23 'end of road right': 'javascripts.directions.instructions.endofroad_right',
24 'end of road left': 'javascripts.directions.instructions.endofroad_left',
25 'turn straight': 'javascripts.directions.instructions.continue',
26 'turn slight right': 'javascripts.directions.instructions.slight_right',
27 'turn right': 'javascripts.directions.instructions.turn_right',
28 'turn sharp right': 'javascripts.directions.instructions.sharp_right',
29 'turn uturn': 'javascripts.directions.instructions.uturn',
30 'turn sharp left': 'javascripts.directions.instructions.sharp_left',
31 'turn left': 'javascripts.directions.instructions.turn_left',
32 'turn slight left': 'javascripts.directions.instructions.slight_left',
33 'trun straight': 'javascripts.directions.instructions.follow',
34 'roundabout': 'javascripts.directions.instructions.roundabout',
35 'rotary': 'javascripts.directions.instructions.roundabout',
36 'depart': 'javascripts.directions.instructions.start',
37 'arrive': 'javascripts.directions.instructions.destination',
49 'end of road right': 22,
50 'end of road left': 23,
52 'turn slight right': 1,
54 'turn sharp right': 3,
56 'turn slight left': 5,
65 var transformed_steps = input_steps.map(function(step, idx) {
68 // special case handling
69 switch (step.maneuver.type) {
75 maneuver_id = step.maneuver.type + ' ' + (step.maneuver.modifier.indexOf('left') >= 0 ? 'left' : 'right');
81 maneuver_id = step.maneuver.type;
83 case 'roundabout turn':
85 maneuver_id = "turn " + step.maneuver.modifier;
87 // for unknown types the fallback is turn
89 maneuver_id = "turn " + step.maneuver.modifier;
92 var template = INSTRUCTION_TEMPLATE[maneuver_id];
94 // convert lat,lng pairs to LatLng objects
95 var step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(function(a) { return L.latLng(a); }) ;
96 // append step_geometry on line
97 Array.prototype.push.apply(line, step_geometry);
99 var instText = "<b>" + (idx + 1) + ".</b> ";
100 var name = step.name ? "<b>" + step.name + "</b>" : I18n.t('javascripts.directions.instructions.unnamed');
101 if (step.maneuver.type.match(/rotary|roundabout/)) {
102 instText += I18n.t(template + '_with_exit', { exit: step.maneuver.exit, name: name } );
104 instText += I18n.t(template + '_without_exit', { name: name });
106 return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
109 return transformed_steps;
112 getRoute: function (points, callback) {
115 { name: "overview", value: "false" },
116 { name: "geometries", value: "polyline" },
117 { name: "steps", value: true }
121 if (cachedHints.length === points.length) {
122 params.push({name: "hints", value: cachedHints.join(";")});
128 var encoded_coords = points.map(function(p) {
129 return p.lng + ',' + p.lat;
132 var req_url = document.location.protocol + OSM.OSRM_URL + encoded_coords;
134 var onResponse = function (data) {
135 if (data.code !== 'Ok')
136 return callback(true);
138 cachedHints = data.waypoints.map(function(wp) {
143 var transformLeg = function (leg) {
144 return this._transformSteps(leg.steps, line);
147 var steps = [].concat.apply([], data.routes[0].legs.map(transformLeg.bind(this)));
152 distance: data.routes[0].distance,
153 time: data.routes[0].duration
161 success: onResponse.bind(this),
170 OSM.Directions.addEngine(new OSRMEngine(), true);