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