2 // Doesn't yet support hints
5 function FOSSGISOSRMEngine(id, vehicleType) {
10 creditline: "<a href=\"https://routing.openstreetmap.de/about.html\" target=\"_blank\">OSRM (FOSSGIS)</a>",
13 _transformSteps: function (input_steps, line) {
14 var INSTRUCTION_TEMPLATE = {
15 "continue": "javascripts.directions.instructions.continue",
16 "merge right": "javascripts.directions.instructions.merge_right",
17 "merge left": "javascripts.directions.instructions.merge_left",
18 "off ramp right": "javascripts.directions.instructions.offramp_right",
19 "off ramp left": "javascripts.directions.instructions.offramp_left",
20 "on ramp right": "javascripts.directions.instructions.onramp_right",
21 "on ramp left": "javascripts.directions.instructions.onramp_left",
22 "fork right": "javascripts.directions.instructions.fork_right",
23 "fork left": "javascripts.directions.instructions.fork_left",
24 "end of road right": "javascripts.directions.instructions.endofroad_right",
25 "end of road left": "javascripts.directions.instructions.endofroad_left",
26 "turn straight": "javascripts.directions.instructions.continue",
27 "turn slight right": "javascripts.directions.instructions.slight_right",
28 "turn right": "javascripts.directions.instructions.turn_right",
29 "turn sharp right": "javascripts.directions.instructions.sharp_right",
30 "turn uturn": "javascripts.directions.instructions.uturn",
31 "turn sharp left": "javascripts.directions.instructions.sharp_left",
32 "turn left": "javascripts.directions.instructions.turn_left",
33 "turn slight left": "javascripts.directions.instructions.slight_left",
34 "roundabout": "javascripts.directions.instructions.roundabout",
35 "rotary": "javascripts.directions.instructions.roundabout",
36 "exit roundabout": "javascripts.directions.instructions.exit_roundabout",
37 "exit rotary": "javascripts.directions.instructions.exit_roundabout",
38 "depart": "javascripts.directions.instructions.start",
39 "arrive": "javascripts.directions.instructions.destination"
51 "end of road right": 22,
52 "end of road left": 23,
54 "turn slight right": 1,
56 "turn sharp right": 3,
58 "turn slight left": 5,
63 "exit roundabout": 10,
68 var numToWord = function (num) {
69 return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1];
71 var transformed_steps = input_steps.map(function (step, idx) {
74 // special case handling
75 switch (step.maneuver.type) {
81 maneuver_id = step.maneuver.type + " " + (step.maneuver.modifier.indexOf("left") >= 0 ? "left" : "right");
87 case "exit roundabout":
89 maneuver_id = step.maneuver.type;
91 case "roundabout turn":
93 maneuver_id = "turn " + step.maneuver.modifier;
95 // for unknown types the fallback is turn
97 maneuver_id = "turn " + step.maneuver.modifier;
100 var template = INSTRUCTION_TEMPLATE[maneuver_id];
102 // convert lat,lng pairs to LatLng objects
103 var step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(function (a) { return L.latLng(a); });
104 // append step_geometry on line
105 Array.prototype.push.apply(line, step_geometry);
107 var instText = "<b>" + (idx + 1) + ".</b> ";
108 var destinations = "<b>" + step.destinations + "</b>";
109 var namedRoad = true;
112 if (step.name && step.ref) {
113 name = "<b>" + step.name + " (" + step.ref + ")</b>";
114 } else if (step.name) {
115 name = "<b>" + step.name + "</b>";
116 } else if (step.ref) {
117 name = "<b>" + step.ref + "</b>";
119 name = I18n.t("javascripts.directions.instructions.unnamed");
123 if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) {
124 instText += I18n.t(template, { name: name });
125 } else if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
126 if (step.maneuver.exit) {
127 if (step.maneuver.exit <= 10) {
128 instText += I18n.t(template + "_with_exit_ordinal", { exit: I18n.t("javascripts.directions.instructions.exit_counts." + numToWord(step.maneuver.exit)), name: name });
130 instText += I18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
133 instText += I18n.t(template + "_without_exit", { name: name });
135 } else if (step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
137 if (step.exits && step.maneuver.type.match(/^(off ramp)$/)) params.exit = step.exits;
138 if (step.destinations) params.directions = destinations;
139 if (namedRoad) params.directions = name;
140 if (Object.keys(params).length > 0) {
141 template = template + "_with_" + Object.keys(params).join("_");
143 instText += I18n.t(template, params);
145 instText += I18n.t(template + "_without_exit", { name: name });
147 return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
150 return transformed_steps;
153 getRoute: function (points, callback) {
155 { name: "overview", value: "false" },
156 { name: "geometries", value: "polyline" },
157 { name: "steps", value: true }
161 if (cachedHints.length === points.length) {
162 params.push({ name: "hints", value: cachedHints.join(";") });
168 var encoded_coords = points.map(function (p) {
169 return p.lng + "," + p.lat;
172 var req_url = OSM.FOSSGIS_OSRM_URL + "routed-" + vehicleType + "/route/v1/driving/" + encoded_coords;
174 var onResponse = function (data) {
175 if (data.code !== "Ok") {
176 return callback(true);
179 cachedHints = data.waypoints.map(function (wp) {
184 var transformLeg = function (leg) {
185 return this._transformSteps(leg.steps, line);
188 var steps = [].concat.apply([], data.routes[0].legs.map(transformLeg.bind(this)));
193 distance: data.routes[0].distance,
194 time: data.routes[0].duration
202 success: onResponse.bind(this),
211 OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_car", "car"), true);
212 OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_bike", "bike"), true);
213 OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_foot", "foot"), true);