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