]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/routing_engines/graphhopper_bicycle.js
Improve error-handling
[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             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         return true;
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 });