]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/mapquest.js
Fix rubocop issue
[rails.git] / app / assets / javascripts / index / directions / 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 function MapQuestEngine(id, routeType) {
7   var MQ_SPRITE_MAP = {
8     0: 0, // straight
9     1: 1, // slight right
10     2: 2, // right
11     3: 3, // sharp right
12     4: 4, // reverse
13     5: 7, // sharp left
14     6: 6, // left
15     7: 5, // slight left
16     8: 4, // right U-turn
17     9: 4, // left U-turn
18     10: 21, // right merge
19     11: 20, // left merge
20     12: 21, // right on-ramp
21     13: 20, // left on-ramp
22     14: 24, // right off-ramp
23     15: 25, // left off-ramp
24     16: 18, // right fork
25     17: 19, // left fork
26     18: 0  // straight fork
27   };
28
29   return {
30     id: id,
31     creditline: '<a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="' + document.location.protocol + '//developer.mapquest.com/content/osm/mq_logo.png">',
32     draggable: false,
33
34     getRoute: function (points, callback) {
35       var from = points[0];
36       var to = points[points.length - 1];
37
38       return $.ajax({
39         url: document.location.protocol + OSM.MAPQUEST_DIRECTIONS_URL,
40         data: {
41           key: OSM.MAPQUEST_KEY,
42           from: from.lat + "," + from.lng,
43           to: to.lat + "," + to.lng,
44           routeType: routeType,
45           // locale: I18n.currentLocale(), //Doesn't actually work. MapQuest requires full locale e.g. "de_DE", but I18n may only provides language, e.g. "de"
46           manMaps: false,
47           shapeFormat: "raw",
48           generalize: 0,
49           unit: "k"
50         },
51         dataType: "jsonp",
52         success: function (data) {
53           if (data.info.statuscode !== 0)
54             return callback(true);
55
56           var i;
57           var line = [];
58           var shape = data.route.shape.shapePoints;
59           for (i = 0; i < shape.length; i += 2) {
60             line.push(L.latLng(shape[i], shape[i + 1]));
61           }
62
63           // data.route.shape.maneuverIndexes links turns to polyline positions
64           // data.route.legs[0].maneuvers is list of turns
65           var steps = [];
66           var mq = data.route.legs[0].maneuvers;
67           for (i = 0; i < mq.length; i++) {
68             var s = mq[i];
69             var d;
70             var linesegstart, linesegend, lineseg;
71             linesegstart = data.route.shape.maneuverIndexes[i];
72             if (i === mq.length - 1) {
73               d = 15;
74               linesegend = linesegstart + 1;
75             } else {
76               d = MQ_SPRITE_MAP[s.turnType];
77               linesegend = data.route.shape.maneuverIndexes[i + 1] + 1;
78             }
79             lineseg = [];
80             for (var j = linesegstart; j < linesegend; j++) {
81               lineseg.push(L.latLng(data.route.shape.shapePoints[j * 2], data.route.shape.shapePoints[j * 2 + 1]));
82             }
83             steps.push([L.latLng(s.startPoint.lat, s.startPoint.lng), d, s.narrative, s.distance * 1000, lineseg]);
84           }
85
86           callback(false, {
87             line: line,
88             steps: steps,
89             distance: data.route.distance * 1000,
90             time: data.route.time
91           });
92         },
93         error: function () {
94           callback(true);
95         }
96       });
97     }
98   };
99 }
100
101 if (OSM.MAPQUEST_KEY) {
102   OSM.Directions.addEngine(new MapQuestEngine("mapquest_bicycle", "bicycle"), true);
103   OSM.Directions.addEngine(new MapQuestEngine("mapquest_foot", "pedestrian"), true);
104   OSM.Directions.addEngine(new MapQuestEngine("mapquest_car", "fastest"), true);
105 }