2 // Doesn't yet support hints
5 function FOSSGISOSRMEngine(modeId, vehicleType) {
8 function getInstructionText(step) {
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"
36 function numToWord(num) {
37 return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1];
40 const instrPrefix = "javascripts.directions.instructions.";
41 let template = instrPrefix + INSTRUCTION_TEMPLATE[step.maneuverId];
43 const destinations = "<b>" + step.destinations + "</b>";
47 if (step.name && step.ref) {
48 name = "<b>" + step.name + " (" + step.ref + ")</b>";
49 } else if (step.name) {
50 name = "<b>" + step.name + "</b>";
51 } else if (step.ref) {
52 name = "<b>" + step.ref + "</b>";
54 name = OSM.i18n.t(instrPrefix + "unnamed");
58 if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) {
59 return OSM.i18n.t(template, { name: name });
61 if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
62 if (!step.maneuver.exit) {
63 return OSM.i18n.t(template + "_without_exit", { name: name });
65 if (step.maneuver.exit > 10) {
66 return OSM.i18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
68 return OSM.i18n.t(template + "_with_exit_ordinal", { exit: OSM.i18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name });
70 if (!step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
71 return OSM.i18n.t(template + "_without_exit", { name: name });
74 if (step.exits && step.maneuver.type.match(/^(off ramp)$/)) params.exit = step.exits;
75 if (step.destinations) params.directions = destinations;
76 if (namedRoad) params.directions = name;
77 if (Object.keys(params).length > 0) {
78 template = template + "_with_" + Object.keys(params).join("_");
80 return OSM.i18n.t(template, params);
83 function _processDirections(leg) {
84 function getManeuverId(maneuver) {
85 // special case handling
86 switch (maneuver.type) {
92 return maneuver.type + " " + (maneuver.modifier.indexOf("left") >= 0 ? "left" : "right");
97 case "exit roundabout":
100 case "roundabout turn":
102 return "turn " + maneuver.modifier;
103 // for unknown types the fallback is turn
105 return "turn " + maneuver.modifier;
109 "continue": "straight",
110 "merge right": "merge-right",
111 "merge left": "merge-left",
112 "off ramp right": "exit-right",
113 "off ramp left": "exit-left",
114 "on ramp right": "right",
115 "on ramp left": "left",
116 "fork right": "fork-right",
117 "fork left": "fork-left",
118 "end of road right": "end-of-road-right",
119 "end of road left": "end-of-road-left",
120 "turn straight": "straight",
121 "turn slight right": "slight-right",
122 "turn right": "right",
123 "turn sharp right": "sharp-right",
124 "turn uturn": "u-turn-left",
125 "turn slight left": "slight-left",
127 "turn sharp left": "sharp-left",
128 "roundabout": "roundabout",
129 "rotary": "roundabout",
130 "exit roundabout": "roundabout",
131 "exit rotary": "roundabout",
133 "arrive": "destination"
136 for (const step of leg.steps) step.maneuverId = getManeuverId(step.maneuver);
138 const steps = leg.steps.map(step => [
139 ICON_MAP[step.maneuverId],
140 getInstructionText(step),
142 L.PolylineUtil.decode(step.geometry, { precision: 5 })
146 line: steps.flatMap(step => step[3]),
148 distance: leg.distance,
150 credit: "OSRM (FOSSGIS)",
151 creditlink: "https://routing.openstreetmap.de/about.html"
157 provider: "fossgis_osrm",
160 getRoute: function (points, signal) {
161 const query = new URLSearchParams({
163 geometries: "polyline",
167 if (cachedHints.length === points.length) {
168 query.set("hints", cachedHints.join(";"));
174 const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";");
176 return fetch(OSM.FOSSGIS_OSRM_URL + req_path + "?" + query, { signal })
177 .then(response => response.json())
179 if (response.code !== "Ok") throw new Error();
180 cachedHints = response.waypoints.map(wp => wp.hint);
181 return _processDirections(response.routes[0].legs[0]);
187 OSM.Directions.addEngine(new FOSSGISOSRMEngine("car", "car"), true);
188 OSM.Directions.addEngine(new FOSSGISOSRMEngine("bicycle", "bike"), true);
189 OSM.Directions.addEngine(new FOSSGISOSRMEngine("foot", "foot"), true);