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