]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/osrm.js
Revert "Don't pass the locale to the Mapzen routing engine"
[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         'javascripts.directions.instructions.ferry'             // 18
33       ];
34
35       var params = [
36         { name: "z", value: "14" },
37         { name: "output", value: "json" },
38         { name: "instructions", value: true }
39       ];
40
41       for (var i = 0; i < points.length; i++) {
42         params.push({ name: "loc", value: points[i].lat + "," + points[i].lng });
43
44         if (hintData && previousPoints && previousPoints[i].equals(points[i])) {
45           params.push({ name: "hint", value: hintData.locations[i] });
46         }
47       }
48
49       if (hintData && hintData.checksum) {
50         params.push({ name: "checksum", value: hintData.checksum });
51       }
52
53       return $.ajax({
54         url: document.location.protocol + OSM.OSRM_URL,
55         data: params,
56         dataType: "json",
57         success: function (data) {
58           if (data.status === 207)
59             return callback(true);
60
61           previousPoints = points;
62           hintData = data.hint_data;
63
64           var line = L.PolylineUtil.decode(data.route_geometry, {
65             precision: 6
66           });
67
68           var steps = [];
69           for (i = 0; i < data.route_instructions.length; i++) {
70             var s = data.route_instructions[i];
71             var linesegend;
72             var instCodes = s[0].split('-');
73             if (s[8] === 2) {
74               /* indicates a ferry in car routing mode, see https://github.com/Project-OSRM/osrm-backend/blob/6cbbd1e5a1b441eb27055f56956e1bac14832a58/profiles/car.lua#L151 */
75               instCodes = ["18"];
76             }
77             var instText = "<b>" + (i + 1) + ".</b> ";
78             var name = s[1] ? "<b>" + s[1] + "</b>" : I18n.t('javascripts.directions.instructions.unnamed');
79             if (instCodes[0] === "11" && instCodes[1]) {
80               instText += I18n.t(TURN_INSTRUCTIONS[instCodes[0]] + '_with_exit', { exit: instCodes[1], name: name } );
81             } else {
82               instText += I18n.t(TURN_INSTRUCTIONS[instCodes[0]] + '_without_exit', { name: name });
83             }
84             if ((i + 1) < data.route_instructions.length) {
85               linesegend = data.route_instructions[i + 1][3] + 1;
86             } else {
87               linesegend = s[3] + 1;
88             }
89             steps.push([line[s[3]], instCodes[0], instText, s[2], line.slice(s[3], linesegend)]);
90           }
91
92           callback(false, {
93             line: line,
94             steps: steps,
95             distance: data.route_summary.total_distance,
96             time: data.route_summary.total_time
97           });
98         },
99         error: function () {
100           callback(true);
101         }
102       });
103     }
104   };
105 }
106
107 OSM.Directions.addEngine(new OSRMEngine(), true);