]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/routing_engines/graphhopper.js
Merge pull request #20 from karussell/routing
[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         getRoute: function(isFinal, points) {
18             // documentation
19             // https://github.com/graphhopper/graphhopper/blob/master/docs/web/api-doc.md
20             var url = "http://graphhopper.com/api/1/route?" 
21                     + that.vehicleParam
22                     + "&locale=" + I18n.currentLocale()
23                     + "&key=LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn";
24             
25             for (var i = 0; i < points.length; i++) {
26                 var pair = points[i].join(',');
27                 url += "&point=" + pair;
28             }            
29             if (isFinal)
30                 url += "&instructions=true";
31             // GraphHopper supports json too
32             this.requestJSONP(url + "&type=jsonp&callback=");
33         },
34         gotRoute: function(router, data) {
35             if (!data.paths || data.paths.length == 0)
36                 return false;
37             
38             // Draw polyline
39             var path = data.paths[0];
40             var line = L.PolylineUtil.decode(path.points);
41             router.setPolyline(line);
42             // Assemble instructions
43             var steps = [];
44             var len = path.instructions.length;
45             for (i = 0; i < len; i++) {
46                 var instr = path.instructions[i];                
47                 var instrCode = (i === len - 1) ? 15 : this.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                 steps.push([{lat: latLng.lat, lng: latLng.lng}, instrCode, instrText, distInMeter, []]); // TODO does graphhopper map instructions onto line indices?
53             }
54             router.setItinerary({ steps: steps, distance: path.distance, time: path.time / 1000 });
55             return true;
56         },
57         GH_INSTR_MAP: {
58             "-3": 6, // sharp left
59             "-2": 7, // left    
60             "-1": 8, // slight left                
61             0: 0, // straight
62             1: 1, // slight right
63             2: 2, // right
64             3: 3, // sharp right
65             4: -1, // finish reached
66             5: -1 // via reached
67         }
68     };
69 };
70
71 OSM.RoutingEngines.add(false, new GraphHopperEngine("Bicycle", "vehicle=bike").createConfig());
72 OSM.RoutingEngines.add(false, new GraphHopperEngine("Foot", "vehicle=foot").createConfig());