2 function GraphHopperEngine(modeId, vehicleType) {
11 "4": "destination", // finish reached
12 "5": "destination", // via reached
16 "-98": "u-turn", // unknown direction u-turn
17 "-8": "u-turn", // left u-turn
18 "8": "u-turn" // right u-turn
21 function _processDirections(path) {
22 const line = L.PolylineUtil.decode(path.points);
24 const steps = path.instructions.map(function (instr, i) {
25 const num = `<b>${i + 1}.</b> `;
27 .slice(instr.interval[0], instr.interval[1] + 1)
28 .map(([lat, lng]) => ({ lat, lng }));
31 GH_INSTR_MAP[instr.sign],
35 ]; // TODO does graphhopper map instructions onto line indices?
37 steps.at(-1)[1] = "destination";
42 distance: path.distance,
43 time: path.time / 1000,
51 provider: "graphhopper",
52 creditline: "<a href=\"https://www.graphhopper.com/\" target=\"_blank\">GraphHopper</a>",
55 getRoute: function (points, signal) {
56 // GraphHopper Directions API documentation
57 // https://graphhopper.com/api/1/docs/routing/
58 const query = new URLSearchParams({
60 locale: I18n.currentLocale(),
61 key: "LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn",
64 turn_costs: vehicleType === "car"
66 points.forEach(p => query.append("point", p.lat + "," + p.lng));
67 return fetch(OSM.GRAPHHOPPER_URL + "?" + query, { signal })
68 .then(response => response.json())
69 .then(({ paths }) => {
70 if (!paths || paths.length === 0) throw new Error();
71 return _processDirections(paths[0]);
77 OSM.Directions.addEngine(new GraphHopperEngine("car", "car"), true);
78 OSM.Directions.addEngine(new GraphHopperEngine("bicycle", "bike"), true);
79 OSM.Directions.addEngine(new GraphHopperEngine("foot", "foot"), true);