]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/graphhopper.js
Merge remote-tracking branch 'upstream/pull/5613'
[rails.git] / app / assets / javascripts / index / directions / graphhopper.js
1 (function () {
2   function GraphHopperEngine(id, vehicleType) {
3     var GH_INSTR_MAP = {
4       "-3": 7, // sharp left
5       "-2": 6, // left
6       "-1": 5, // slight left
7       "0": 0, // straight
8       "1": 1, // slight right
9       "2": 2, // right
10       "3": 3, // sharp right
11       "4": 14, // finish reached
12       "5": 14, // via reached
13       "6": 10, // roundabout
14       "-7": 19, // keep left
15       "7": 18, // keep right
16       "-98": 4, // unknown direction u-turn
17       "-8": 4, // left u-turn
18       "8": 4 // right u-turn
19     };
20
21     return {
22       id: id,
23       creditline: "<a href=\"https://www.graphhopper.com/\" target=\"_blank\">GraphHopper</a>",
24       draggable: false,
25
26       getRoute: function (points, callback) {
27       // GraphHopper Directions API documentation
28       // https://graphhopper.com/api/1/docs/routing/
29         return $.ajax({
30           url: OSM.GRAPHHOPPER_URL,
31           data: {
32             vehicle: vehicleType,
33             locale: I18n.currentLocale(),
34             key: "LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn",
35             elevation: false,
36             instructions: true,
37             turn_costs: vehicleType === "car",
38             point: points.map(function (p) { return p.lat + "," + p.lng; })
39           },
40           traditional: true,
41           dataType: "json",
42           success: function (data) {
43             if (!data.paths || data.paths.length === 0) {
44               return callback(true);
45             }
46
47             var path = data.paths[0];
48             var line = L.PolylineUtil.decode(path.points);
49
50             var steps = [];
51             var len = path.instructions.length;
52             for (var i = 0; i < len; i++) {
53               var instr = path.instructions[i];
54               var instrCode = (i === len - 1) ? 14 : GH_INSTR_MAP[instr.sign];
55               var instrText = "<b>" + (i + 1) + ".</b> ";
56               instrText += instr.text;
57               var latLng = line[instr.interval[0]];
58               var distInMeter = instr.distance;
59               var lineseg = [];
60               for (var j = instr.interval[0]; j <= instr.interval[1]; j++) {
61                 lineseg.push({ lat: line[j][0], lng: line[j][1] });
62               }
63               steps.push([
64                 { lat: latLng[0], lng: latLng[1] },
65                 instrCode,
66                 instrText,
67                 distInMeter,
68                 lineseg
69               ]); // TODO does graphhopper map instructions onto line indices?
70             }
71
72             callback(false, {
73               line: line,
74               steps: steps,
75               distance: path.distance,
76               time: path.time / 1000,
77               ascend: path.ascend,
78               descend: path.descend
79             });
80           },
81           error: function () {
82             callback(true);
83           }
84         });
85       }
86     };
87   }
88
89   OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_car", "car"), true);
90   OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_bicycle", "bike"), true);
91   OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_foot", "foot"), true);
92 }());