]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions_engines/osrm.js
OSM.Routing -> OSM.Directions
[rails.git] / app / assets / javascripts / index / directions_engines / osrm.js
1 // OSRM car engine
2 // Doesn't yet support hints
3
4 OSRMEngine = function (vehicleName, baseURL, locale) {
5   this.vehicleName = vehicleName;
6   this.baseURL = baseURL;
7   this.locale = locale;
8   if (!locale)
9     this.locale = "en";
10 };
11
12 OSRMEngine.prototype.createConfig = function () {
13   var that = this;
14   return {
15     name: "javascripts.directions.engines.osrm_" + this.vehicleName.toLowerCase(),
16     creditline: '<a href="http://project-osrm.org/" target="_blank">OSRM</a>',
17     draggable: true,
18     _hints: {},
19
20     getRoute: function (isFinal, points) {
21       var url = that.baseURL + "?z=14&output=json";
22       for (var i = 0; i < points.length; i++) {
23         var pair = points[i].join(',');
24         url += "&loc=" + pair;
25         if (this._hints[pair]) url += "&hint=" + this._hints[pair];
26       }
27       if (isFinal) url += "&instructions=true";
28       this.requestCORS(url);
29     },
30
31     gotRoute: function (router, data) {
32       if (data.status == 207) {
33         return false;
34       }
35       // Draw polyline
36       var line = L.PolylineUtil.decode(data.route_geometry);
37       for (i = 0; i < line.length; i++) {
38         line[i].lat /= 10;
39         line[i].lng /= 10;
40       }
41       router.setPolyline(line);
42       // Assemble instructions
43       var steps = [];
44       for (i = 0; i < data.route_instructions.length; i++) {
45         var s = data.route_instructions[i];
46         var linesegend;
47         var instCodes = s[0].split('-');
48         var instText = "<b>" + (i + 1) + ".</b> ";
49         instText += TURN_INSTRUCTIONS[instCodes[0]];
50         if (instCodes[1]) {
51           instText += "exit " + instCodes[1] + " ";
52         }
53         if (instCodes[0] != 15) {
54           instText += s[1] ? "<b>" + s[1] + "</b>" : I18n.t('javascripts.directions.instructions.unnamed');
55         }
56         if ((i + 1) < data.route_instructions.length) {
57           linesegend = data.route_instructions[i + 1][3] + 1;
58         } else {
59           linesegend = s[3] + 1;
60         }
61         steps.push([line[s[3]], s[0].split('-')[0], instText, s[2], line.slice(s[3], linesegend)]);
62       }
63       if (steps.length) router.setItinerary({ steps: steps, distance: data.route_summary.total_distance, time: data.route_summary.total_time });
64       return true;
65     }
66   };
67 };
68
69 OSM.DirectionsEngines.add(false, new OSRMEngine("Car", "http://router.project-osrm.org/viaroute").createConfig());