bec855f88dd44e0f1b07f6cd21a537ddf18055ab
[rails.git] / app / assets / javascripts / routing_engines / graphhopper_bicycle.js
1 // GraphHopper bicycle engine
2
3 OSM.RoutingEngines.list.push({    
4     name: "javascripts.directions.engines.graphhopper_bike",
5     creditline: 'Directions courtesy of <a href="http://graphhopper.com/routing/" target="_blank">Graphhopper</a>',
6     draggable: true,
7     _hints: {},
8     getRoute: function(isFinal, points) {
9         var url = "http://graphhopper.com/routing/api/route?vehicle=bike&locale=" + I18n.currentLocale();
10         for (var i = 0; i < points.length; i++) {
11             var pair = points[i].join(',');
12             url += "&point=" + pair;
13         }
14         if (isFinal)
15             url += "&instructions=true";
16         this.requestJSONP(url + "&type=jsonp&callback=");
17     },
18     gotRoute: function(router, data) {
19         if (!data.info.routeFound) {
20             alert("Couldn't find route between those two places");
21             return false;
22         }
23         // Draw polyline
24         var line = L.PolylineUtil.decode(data.route.coordinates);
25         router.setPolyline(line);
26         // Assemble instructions
27         var steps = [];
28         var instr = data.route.instructions;
29         for (i = 0; i < instr.descriptions.length; i++) {
30             var indi = instr.indications[i];
31             var instrCode = (i==instr.descriptions.length-1) ? 15 : this.GH_INSTR_MAP[indi];
32             var instrText = "<b>" + (i + 1) + ".</b> ";
33             instrText += instr.descriptions[i];
34             var latlng = instr.latLngs[i];
35             var distInMeter = instr.distances[i];            
36             steps.push([{lat: latlng[0], lng: latlng[1]}, instrCode, instrText, distInMeter]);
37         }
38         router.setItinerary({steps: steps});
39     },
40     GH_INSTR_MAP: {
41         "-3": 6, // sharp left
42         "-2": 7, // left        
43         "-1": 8, // slight left                
44         0: 0, // straight
45         1: 1, // slight right
46         2: 2, // right
47         3: 3 // sharp right             
48     }
49 });