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 'roundabout': 'javascripts.directions.instructions.roundabout',
34 'rotary': 'javascripts.directions.instructions.roundabout',
35 'depart': 'javascripts.directions.instructions.start',
36 'arrive': 'javascripts.directions.instructions.destination',
48 'end of road right': 22,
49 'end of road left': 23,
51 'turn slight right': 1,
53 'turn sharp right': 3,
55 'turn slight left': 5,
63 var numToWord = function(num) {
64 return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num-1];
66 var transformed_steps = input_steps.map(function(step, idx) {
69 // special case handling
70 switch (step.maneuver.type) {
76 maneuver_id = step.maneuver.type + ' ' + (step.maneuver.modifier.indexOf('left') >= 0 ? 'left' : 'right');
82 maneuver_id = step.maneuver.type;
84 case 'roundabout turn':
86 maneuver_id = "turn " + step.maneuver.modifier;
88 // for unknown types the fallback is turn
90 maneuver_id = "turn " + step.maneuver.modifier;
93 var template = INSTRUCTION_TEMPLATE[maneuver_id];
95 // convert lat,lng pairs to LatLng objects
96 var step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(function(a) { return L.latLng(a); }) ;
97 // append step_geometry on line
98 Array.prototype.push.apply(line, step_geometry);
100 var instText = "<b>" + (idx + 1) + ".</b> ";
101 var destinations = "<b>" + step.destinations + "</b>";
102 var namedRoad = true;
105 if (step.name && step.ref) {
106 name = "<b>" + step.name + " (" + step.ref + ")</b>";
107 } else if (step.name) {
108 name = "<b>" + step.name + "</b>";
109 } else if (step.ref) {
110 name = "<b>" + step.ref + "</b>";
112 name = I18n.t('javascripts.directions.instructions.unnamed');
116 if (step.maneuver.type.match(/rotary|roundabout/)) {
117 if (step.maneuver.exit) {
118 if (step.maneuver.exit <= 10) {
119 instText += I18n.t(template + '_with_exit_ordinal', { exit: I18n.t('javascripts.directions.instructions.exit_counts.' + numToWord(step.maneuver.exit)), name: name });
121 instText += I18n.t(template + '_with_exit', { exit: step.maneuver.exit, name: name });
124 instText += I18n.t(template + '_without_exit', { name: name });
126 } else if (step.maneuver.type.match(/on ramp|off ramp/)) {
128 if (step.exits && step.maneuver.type.match(/off ramp/)) params.exit = step.exits;
129 if (step.destinations) params.directions = destinations;
130 if (namedRoad) params.directions = name;
131 if (Object.keys(params).length > 0) {
132 template = template + "_with_" + Object.keys(params).join("_");
134 instText += I18n.t(template, params);
136 instText += I18n.t(template + '_without_exit', { name: name });
138 return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
141 return transformed_steps;
144 getRoute: function (points, callback) {
147 { name: "overview", value: "false" },
148 { name: "geometries", value: "polyline" },
149 { name: "steps", value: true }
153 if (cachedHints.length === points.length) {
154 params.push({name: "hints", value: cachedHints.join(";")});
160 var encoded_coords = points.map(function(p) {
161 return p.lng + ',' + p.lat;
164 var req_url = OSM.OSRM_URL + encoded_coords;
166 var onResponse = function (data) {
167 if (data.code !== 'Ok')
168 return callback(true);
170 cachedHints = data.waypoints.map(function(wp) {
175 var transformLeg = function (leg) {
176 return this._transformSteps(leg.steps, line);
179 var steps = [].concat.apply([], data.routes[0].legs.map(transformLeg.bind(this)));
184 distance: data.routes[0].distance,
185 time: data.routes[0].duration
193 success: onResponse.bind(this),
202 OSM.Directions.addEngine(new OSRMEngine(), true);