]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/osrm.js
4a42d5dc96ea1e8d138c353afbd28824dc7bbdba
[rails.git] / app / assets / javascripts / index / directions / osrm.js
1 // OSRM car engine
2 // Doesn't yet support hints
3
4 function OSRMEngine() {
5   var previousPoints, hintData;
6
7   return {
8     id: "osrm_car",
9     creditline: '<a href="http://project-osrm.org/" target="_blank">OSRM</a>',
10     draggable: true,
11
12     getRoute: function (points, callback) {
13       var TURN_INSTRUCTIONS = [
14         "",
15         'javascripts.directions.instructions.continue',         // 1
16         'javascripts.directions.instructions.slight_right',     // 2
17         'javascripts.directions.instructions.turn_right',       // 3
18         'javascripts.directions.instructions.sharp_right',      // 4
19         'javascripts.directions.instructions.uturn',            // 5
20         'javascripts.directions.instructions.sharp_left',       // 6
21         'javascripts.directions.instructions.turn_left',        // 7
22         'javascripts.directions.instructions.slight_left',      // 8
23         'javascripts.directions.instructions.via_point',        // 9
24         'javascripts.directions.instructions.follow',           // 10
25         'javascripts.directions.instructions.roundabout',       // 11
26         'javascripts.directions.instructions.leave_roundabout', // 12
27         'javascripts.directions.instructions.stay_roundabout',  // 13
28         'javascripts.directions.instructions.start',            // 14
29         'javascripts.directions.instructions.destination',      // 15
30         'javascripts.directions.instructions.against_oneway',   // 16
31         'javascripts.directions.instructions.end_oneway'        // 17
32       ];
33
34       var params = [
35         { name: "z", value: "14" },
36         { name: "output", value: "json" },
37         { name: "instructions", value: true }
38       ];
39
40       for (var i = 0; i < points.length; i++) {
41         params.push({ name: "loc", value: points[i].lat + "," + points[i].lng });
42
43         if (hintData && previousPoints && previousPoints[i].equals(points[i])) {
44           params.push({ name: "hint", value: hintData.locations[i] });
45         }
46       }
47
48       if (hintData && hintData.checksum) {
49         params.push({ name: "checksum", value: hintData.checksum });
50       }
51
52       return $.ajax({
53         url: document.location.protocol + "//router.project-osrm.org/viaroute",
54         data: params,
55         dataType: "json",
56         success: function (data) {
57           if (data.status === 207)
58             return callback(true);
59
60           previousPoints = points;
61           hintData = data.hint_data;
62
63           var line = L.PolylineUtil.decode(data.route_geometry, {
64             precision: 6
65           });
66
67           var steps = [];
68           for (i = 0; i < data.route_instructions.length; i++) {
69             var s = data.route_instructions[i];
70             var linesegend;
71             var instCodes = s[0].split('-');
72             var instText = "<b>" + (i + 1) + ".</b> ";
73             var name = s[1] ? "<b>" + s[1] + "</b>" : I18n.t('javascripts.directions.instructions.unnamed');
74             if (instCodes[0] === "11" && instCodes[1]) {
75               instText += I18n.t(TURN_INSTRUCTIONS[instCodes[0]] + '_with_exit', { exit: instCodes[1], name: name } );
76             } else {
77               instText += I18n.t(TURN_INSTRUCTIONS[instCodes[0]] + '_without_exit', { name: name });
78             }
79             if ((i + 1) < data.route_instructions.length) {
80               linesegend = data.route_instructions[i + 1][3] + 1;
81             } else {
82               linesegend = s[3] + 1;
83             }
84             steps.push([line[s[3]], s[0].split('-')[0], instText, s[2], line.slice(s[3], linesegend)]);
85           }
86
87           callback(false, {
88             line: line,
89             steps: steps,
90             distance: data.route_summary.total_distance,
91             time: data.route_summary.total_time
92           });
93         },
94         error: function () {
95           callback(true);
96         }
97       });
98     }
99   };
100 }
101
102 OSM.Directions.addEngine(new OSRMEngine(), true);