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