]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/routing_engines/graphhopper.js
0f29c162b8ffe57da6ed290aef3577da15159560
[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             var url = "http://graphhopper.com/routing/api/route?" 
19                     + that.vehicleParam 
20                     + "&locale=" + I18n.currentLocale();
21             for (var i = 0; i < points.length; i++) {
22                 var pair = points[i].join(',');
23                 url += "&point=" + pair;
24             }            
25             if (isFinal)
26                 url += "&instructions=true";
27             // GraphHopper supports json too
28             this.requestJSONP(url + "&type=jsonp&callback=");
29         },
30         gotRoute: function(router, data) {
31             if (!data.info.routeFound) {
32                 return false;
33             }
34             // Draw polyline
35             var line = L.PolylineUtil.decode(data.route.coordinates);
36             router.setPolyline(line);
37             // Assemble instructions
38             var steps = [];
39             var instr = data.route.instructions;
40             for (i = 0; i < instr.descriptions.length; i++) {
41                 var indi = instr.indications[i];
42                 var instrCode = (i == instr.descriptions.length - 1) ? 15 : this.GH_INSTR_MAP[indi];
43                 var instrText = "<b>" + (i + 1) + ".</b> ";
44                 instrText += instr.descriptions[i];
45                 var latlng = instr.latLngs[i];
46                 var distInMeter = instr.distances[i];
47                 steps.push([{lat: latlng[0], lng: latlng[1]}, instrCode, instrText, distInMeter, []]); // TODO does graphhopper map instructions onto line indices?
48             }
49             router.setItinerary({ steps: steps, distance: data.route.distance, time: data.route['time']/1000 });
50             return true;
51         },
52         GH_INSTR_MAP: {
53             "-3": 6, // sharp left
54             "-2": 7, // left    
55             "-1": 8, // slight left                
56             0: 0, // straight
57             1: 1, // slight right
58             2: 2, // right
59             3: 3 // sharp right         
60         }
61     };
62 };
63
64 OSM.RoutingEngines.add(false, new GraphHopperEngine("Bicycle", "vehicle=bike").createConfig());
65 OSM.RoutingEngines.add(false, new GraphHopperEngine("Foot", "vehicle=foot").createConfig());