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