da94adf053aa8356c385a1aff8b9ad9562850545
[rails.git] / app / assets / javascripts / routing_engines / osrm_car.js
1 // OSRM car engine
2 // *** this should all be shared from an OSRM library somewhere
3 // *** need to clear hints at some point
4
5 OSM.RoutingEngines.list.push({
6         name: "javascripts.directions.engines.osrm_car",
7         creditline: 'Directions courtesy of <a href="http://project-osrm.org/" target="_blank">OSRM</a>',
8         draggable: true,
9         _hints: {},
10         getRoute: function(isFinal,points) {
11                 var url="http://router.project-osrm.org/viaroute?z=14&output=json";
12                 for (var i=0; i<points.length; i++) {
13                         var pair=points[i].join(',');
14                         url+="&loc="+pair;
15                         if (this._hints[pair]) url+= "&hint="+this._hints[pair];
16                 }
17                 if (isFinal) url+="&instructions=true";
18                 this.requestJSONP(url+"&jsonp=");
19         },
20         gotRoute: function(router,data) {
21                 if (data.status==207) {
22                         alert("Couldn't find route between those two places");
23                         return false;
24                 }
25                 // Draw polyline
26                 var line=L.PolylineUtil.decode(data.route_geometry);
27                 for (i=0; i<line.length; i++) { line[i].lat/=10; line[i].lng/=10; }
28                 router.setPolyline(line);
29                 // *** store hints
30                 // Assemble instructions
31                 var steps=[];
32                 for (i=0; i<data.route_instructions.length; i++) {
33                         var s=data.route_instructions[i];
34                         var instCodes=s[0].split('-');
35                         var instText="<b>"+(i+1)+".</b> ";
36                         instText+=TURN_INSTRUCTIONS[instCodes[0]];
37                         if (instCodes[1]) { instText+="exit "+instCodes[1]+" "; }
38                         if (instCodes[0]!=15) { instText+=s[1] ? "<b>"+s[1]+"</b>" : I18n.t('javascripts.directions.instructions.unnamed'); }
39                         steps.push([line[s[3]], s[0].split('-')[0], instText, s[2]]);
40                 }
41                 if (steps.length) router.setItinerary({ steps: steps });
42         }
43 });