]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/routing_engines/graphhopper.js
Conform to project style conventions
[rails.git] / app / assets / javascripts / routing_engines / graphhopper.js
1 GraphHopperEngine = function (vehicleName, vehicleParam, locale) {
2   this.vehicleName = vehicleName;
3   this.vehicleParam = vehicleParam;
4   //At this point the local system isn't correctly initialised yet, so we don't have accurate information about current locale
5   this.locale = locale;
6   if (!locale)
7     this.locale = "en";
8 };
9
10 GraphHopperEngine.prototype.createConfig = function () {
11   var that = this;
12   return {
13     name: "javascripts.directions.engines.graphhopper_" + this.vehicleName.toLowerCase(),
14     creditline: '<a href="http://graphhopper.com/" target="_blank">Graphhopper</a>',
15     draggable: false,
16     _hints: {},
17
18     getRoute: function (isFinal, points) {
19       // documentation
20       // https://github.com/graphhopper/graphhopper/blob/master/docs/web/api-doc.md
21       var url = "http://graphhopper.com/api/1/route?"
22         + that.vehicleParam
23         + "&locale=" + I18n.currentLocale()
24         + "&key=LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn";
25
26       for (var i = 0; i < points.length; i++) {
27         var pair = points[i].join(',');
28         url += "&point=" + pair;
29       }
30       if (isFinal)
31         url += "&instructions=true";
32       // GraphHopper supports json too
33       this.requestJSONP(url + "&type=jsonp&callback=");
34     },
35
36     gotRoute: function (router, data) {
37       if (!data.paths || data.paths.length == 0)
38         return false;
39
40       // Draw polyline
41       var path = data.paths[0];
42       var line = L.PolylineUtil.decode(path.points);
43       router.setPolyline(line);
44       // Assemble instructions
45       var steps = [];
46       var len = path.instructions.length;
47       for (i = 0; i < len; i++) {
48         var instr = path.instructions[i];
49         var instrCode = (i === len - 1) ? 15 : this.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         steps.push([
55           {lat: latLng.lat, lng: latLng.lng},
56           instrCode,
57           instrText,
58           distInMeter,
59           []
60         ]); // TODO does graphhopper map instructions onto line indices?
61       }
62       router.setItinerary({ steps: steps, distance: path.distance, time: path.time / 1000 });
63       return true;
64     },
65
66     GH_INSTR_MAP: {
67       "-3": 6, // sharp left
68       "-2": 7, // left
69       "-1": 8, // slight left
70       0: 0, // straight
71       1: 1, // slight right
72       2: 2, // right
73       3: 3, // sharp right
74       4: -1, // finish reached
75       5: -1 // via reached
76     }
77   };
78 };
79
80 OSM.RoutingEngines.add(false, new GraphHopperEngine("Bicycle", "vehicle=bike").createConfig());
81 OSM.RoutingEngines.add(false, new GraphHopperEngine("Foot", "vehicle=foot").createConfig());