]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index_modules/directions/engines/graphhopper.js
Merge pull request #7191 from CommanderStorm/fix-attribution-listener-leak
[rails.git] / app / assets / javascripts / index_modules / directions / engines / graphhopper.js
1 //= require polyline_decoder
2
3 (function () {
4   function GraphHopperEngine(modeId, vehicleType, profile) {
5     const GH_INSTR_MAP = {
6       "-3": "sharp-left",
7       "-2": "left",
8       "-1": "slight-left",
9       "0": "straight",
10       "1": "slight-right",
11       "2": "right",
12       "3": "sharp-right",
13       "4": "destination", // finish reached
14       "5": "destination", // via reached
15       "6": "roundabout",
16       "-7": "fork-left",
17       "7": "fork-right",
18       "-98": "u-turn-left", // unknown direction u-turn
19       "-8": "u-turn-left", // left u-turn
20       "8": "u-turn-right" // right u-turn
21     };
22
23     function _processDirections(path) {
24       const line = OSM.decodePolyline(path.points, { precision: 5 });
25
26       const steps = path.instructions.map(instr => [
27         GH_INSTR_MAP[instr.sign],
28         instr.text,
29         instr.distance,
30         line.slice(instr.interval[0], instr.interval[1] + 1)
31       ]);
32
33       steps.at(-1)[0] = "destination";
34
35       return {
36         line,
37         steps,
38         distance: path.distance,
39         time: path.time / 1000,
40         ascend: path.ascend,
41         descend: path.descend
42       };
43     }
44
45     return {
46       mode: modeId,
47       provider: "graphhopper",
48       draggable: false,
49
50       getRoute: function (points, signal) {
51         // GraphHopper Directions API documentation https://docs.graphhopper.com
52         const query = new URLSearchParams({
53           profile: vehicleType,
54           locale: OSM.i18n.locale,
55           key: "7cb4eb19-e0f4-40a3-a5e0-f2c039366f32",
56           elevation: false,
57           instructions: true
58         });
59         const demoQuery = new URLSearchParams({ profile });
60
61         for (const { lat, lng } of points) {
62           query.append("point", [lat, lng]);
63           demoQuery.append("point", [lat, lng]);
64         }
65
66         const meta = {
67           credit: "GraphHopper",
68           creditlink: "https://www.graphhopper.com/",
69           demolink: "https://graphhopper.com/maps/?" + demoQuery
70         };
71
72         return fetch(OSM.GRAPHHOPPER_URL + "?" + query, { signal })
73           .then(response => response.json())
74           .then(({ paths }) => {
75             if (!paths || paths.length === 0) throw new Error();
76
77             return { ..._processDirections(paths[0]), ...meta };
78           });
79       }
80     };
81   }
82
83   OSM.directionsEngines.add(new GraphHopperEngine("car", "car", "car"), true);
84   OSM.directionsEngines.add(new GraphHopperEngine("bicycle", "bike", "bike"), true);
85   OSM.directionsEngines.add(new GraphHopperEngine("foot", "foot", "foot"), true);
86 }());