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