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