]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/routing_engines/mapquest.js
Conform to project style conventions
[rails.git] / app / assets / javascripts / routing_engines / mapquest.js
1 // For docs, see:
2 // http://developer.mapquest.com/web/products/open/directions-service
3 // http://open.mapquestapi.com/directions/
4 // https://github.com/apmon/openstreetmap-website/blob/21edc353a4558006f0ce23f5ec3930be6a7d4c8b/app/controllers/routing_controller.rb#L153
5
6 MapQuestEngine = function (vehicleName, vehicleParam, locale) {
7   this.vehicleName = vehicleName;
8   this.vehicleParam = vehicleParam;
9   this.locale = locale;
10   if (!locale)
11     this.locale = "en";
12 };
13
14 MapQuestEngine.prototype.createConfig = function () {
15   var that = this;
16   return {
17     name: "javascripts.directions.engines.mapquest_" + this.vehicleName.toLowerCase(),
18     creditline: '<a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">',
19     draggable: false,
20     _hints: {},
21
22     MQ_SPRITE_MAP: {
23       0: 1, // straight
24       1: 2, // slight right
25       2: 3, // right
26       3: 4, // sharp right
27       4: 5, // reverse
28       5: 6, // sharp left
29       6: 7, // left
30       7: 8, // slight left
31       8: 5, // right U-turn
32       9: 5, // left U-turn
33       10: 2, // right merge
34       11: 8, // left merge
35       12: 2, // right on-ramp
36       13: 8, // left on-ramp
37       14: 2, // right off-ramp
38       15: 8, // left off-ramp
39       16: 2, // right fork
40       17: 8, // left fork
41       18: 1  // straight fork
42     },
43
44     getRoute: function (isFinal, points) {
45       var url = document.location.protocol + "//open.mapquestapi.com/directions/v2/route?key=Fmjtd%7Cluur290anu%2Crl%3Do5-908a0y";
46       var from = points[0];
47       var to = points[points.length - 1];
48       url += "&from=" + from.join(',');
49       url += "&to=" + to.join(',');
50       url += "&" + that.vehicleParam;
51       //url+="&locale=" + I18n.currentLocale(); //Doesn't actually work. MapQuest requires full locale e.g. "de_DE", but I18n may only provides language, e.g. "de"
52       url += "&manMaps=false";
53       url += "&shapeFormat=raw&generalize=0&unit=k";
54       this.requestCORS(url);
55     },
56
57     gotRoute: function (router, data) {
58       if (data.info.statuscode != 0) return false;
59
60       var poly = [];
61       var shape = data.route.shape.shapePoints;
62       for (var i = 0; i < shape.length; i += 2) {
63         poly.push(L.latLng(shape[i], shape[i + 1]));
64       }
65       router.setPolyline(poly);
66
67       // data.route.shape.maneuverIndexes links turns to polyline positions
68       // data.route.legs[0].maneuvers is list of turns
69       var steps = [];
70       var mq = data.route.legs[0].maneuvers;
71       for (var i = 0; i < mq.length; i++) {
72         var s = mq[i];
73         var d;
74         var linesegstart, linesegend, lineseg;
75         linesegstart = data.route.shape.maneuverIndexes[i];
76         if (i == mq.length - 1) {
77           d = 15;
78           linesegend = linesegstart + 1;
79         } else {
80           d = this.MQ_SPRITE_MAP[s.turnType];
81           linesegend = data.route.shape.maneuverIndexes[i + 1] + 1;
82         }
83         lineseg = [];
84         for (var j = linesegstart; j < linesegend; j++) {
85           lineseg.push(L.latLng(data.route.shape.shapePoints[j * 2], data.route.shape.shapePoints[j * 2 + 1]));
86         }
87         steps.push([L.latLng(s.startPoint.lat, s.startPoint.lng), d, s.narrative, s.distance * 1000, lineseg]);
88       }
89       router.setItinerary({ steps: steps, distance: data.route.distance * 1000, time: data.route['time'] });
90       return true;
91     }
92   };
93 };
94
95 OSM.RoutingEngines.add(true, new MapQuestEngine("Bicycle", "routeType=bicycle").createConfig());
96 OSM.RoutingEngines.add(true, new MapQuestEngine("Foot", "routeType=pedestrian").createConfig());
97 OSM.RoutingEngines.add(true, new MapQuestEngine("Car", "routeType=fastest").createConfig());
98 // can be: routeType=fastest|shortest|pedestrian|multimodal|bicycle