2 // Doesn't yet support hints
5 function FOSSGISOSRMEngine(id, vehicleType) {
8 function _processDirections(route) {
9 const INSTRUCTION_TEMPLATE = {
10 "continue": "continue",
11 "merge right": "merge_right",
12 "merge left": "merge_left",
13 "off ramp right": "offramp_right",
14 "off ramp left": "offramp_left",
15 "on ramp right": "onramp_right",
16 "on ramp left": "onramp_left",
17 "fork right": "fork_right",
18 "fork left": "fork_left",
19 "end of road right": "endofroad_right",
20 "end of road left": "endofroad_left",
21 "turn straight": "continue",
22 "turn slight right": "slight_right",
23 "turn right": "turn_right",
24 "turn sharp right": "sharp_right",
25 "turn uturn": "uturn",
26 "turn sharp left": "sharp_left",
27 "turn left": "turn_left",
28 "turn slight left": "slight_left",
29 "roundabout": "roundabout",
30 "rotary": "roundabout",
31 "exit roundabout": "exit_roundabout",
32 "exit rotary": "exit_roundabout",
34 "arrive": "destination"
46 "end of road right": 22,
47 "end of road left": 23,
49 "turn slight right": 1,
51 "turn sharp right": 3,
53 "turn slight left": 5,
58 "exit roundabout": 10,
63 function numToWord(num) {
64 return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1];
66 function getManeuverId(maneuver) {
67 // special case handling
68 switch (maneuver.type) {
74 return maneuver.type + " " + (maneuver.modifier.indexOf("left") >= 0 ? "left" : "right");
79 case "exit roundabout":
82 case "roundabout turn":
84 return "turn " + maneuver.modifier;
85 // for unknown types the fallback is turn
87 return "turn " + maneuver.modifier;
91 const steps = route.legs.flatMap(
92 leg => leg.steps.map(function (step, idx) {
93 const maneuver_id = getManeuverId(step.maneuver);
95 const instrPrefix = "javascripts.directions.instructions.";
96 let template = instrPrefix + INSTRUCTION_TEMPLATE[maneuver_id];
98 const step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(L.latLng);
100 let instText = "<b>" + (idx + 1) + ".</b> ";
101 const destinations = "<b>" + step.destinations + "</b>";
102 let 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(instrPrefix + "unnamed");
116 if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) {
117 instText += I18n.t(template, { name: name });
118 } else if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
119 if (step.maneuver.exit) {
120 if (step.maneuver.exit <= 10) {
121 instText += I18n.t(template + "_with_exit_ordinal", { exit: I18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name });
123 instText += I18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
126 instText += I18n.t(template + "_without_exit", { name: name });
128 } else if (step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
130 if (step.exits && step.maneuver.type.match(/^(off ramp)$/)) params.exit = step.exits;
131 if (step.destinations) params.directions = destinations;
132 if (namedRoad) params.directions = name;
133 if (Object.keys(params).length > 0) {
134 template = template + "_with_" + Object.keys(params).join("_");
136 instText += I18n.t(template, params);
138 instText += I18n.t(template + "_without_exit", { name: name });
140 return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
145 line: steps.flatMap(step => step[4]),
147 distance: route.distance,
154 creditline: "<a href=\"https://routing.openstreetmap.de/about.html\" target=\"_blank\">OSRM (FOSSGIS)</a>",
157 getRoute: function (points, callback) {
159 { name: "overview", value: "false" },
160 { name: "geometries", value: "polyline" },
161 { name: "steps", value: true }
164 if (cachedHints.length === points.length) {
165 data.push({ name: "hints", value: cachedHints.join(";") });
171 const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";");
174 url: OSM.FOSSGIS_OSRM_URL + req_path,
177 success: function (response) {
178 if (response.code !== "Ok") {
179 return callback(true);
182 cachedHints = response.waypoints.map(wp => wp.hint);
183 callback(false, _processDirections(response.routes[0]));
193 OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_car", "car"), true);
194 OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_bike", "bike"), true);
195 OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_foot", "foot"), true);