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