]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/mapquest.js
Move the MapQuest API key to the configuration file
[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, vehicleParam) {
7   var MQ_SPRITE_MAP = {
8     0: 1, // straight
9     1: 2, // slight right
10     2: 3, // right
11     3: 4, // sharp right
12     4: 5, // reverse
13     5: 6, // sharp left
14     6: 7, // left
15     7: 8, // slight left
16     8: 5, // right U-turn
17     9: 5, // left U-turn
18     10: 2, // right merge
19     11: 8, // left merge
20     12: 2, // right on-ramp
21     13: 8, // left on-ramp
22     14: 2, // right off-ramp
23     15: 8, // left off-ramp
24     16: 2, // right fork
25     17: 8, // left fork
26     18: 1  // 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 url = document.location.protocol + "//open.mapquestapi.com/directions/v2/route";
36       var from = points[0];
37       var to = points[points.length - 1];
38       url += "?key=" + OSM.MAPQUEST_KEY;
39       url += "&from=" + from.lat + ',' + from.lng;
40       url += "&to=" + to.lat + ',' + to.lng;
41       url += "&" + vehicleParam;
42       //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"
43       url += "&manMaps=false";
44       url += "&shapeFormat=raw&generalize=0&unit=k";
45
46       return $.ajax({
47         url: url,
48         success: function (data) {
49           if (data.info.statuscode !== 0)
50             return callback(true);
51
52           var i;
53           var line = [];
54           var shape = data.route.shape.shapePoints;
55           for (i = 0; i < shape.length; i += 2) {
56             line.push(L.latLng(shape[i], shape[i + 1]));
57           }
58
59           // data.route.shape.maneuverIndexes links turns to polyline positions
60           // data.route.legs[0].maneuvers is list of turns
61           var steps = [];
62           var mq = data.route.legs[0].maneuvers;
63           for (i = 0; i < mq.length; i++) {
64             var s = mq[i];
65             var d;
66             var linesegstart, linesegend, lineseg;
67             linesegstart = data.route.shape.maneuverIndexes[i];
68             if (i === mq.length - 1) {
69               d = 15;
70               linesegend = linesegstart + 1;
71             } else {
72               d = MQ_SPRITE_MAP[s.turnType];
73               linesegend = data.route.shape.maneuverIndexes[i + 1] + 1;
74             }
75             lineseg = [];
76             for (var j = linesegstart; j < linesegend; j++) {
77               lineseg.push(L.latLng(data.route.shape.shapePoints[j * 2], data.route.shape.shapePoints[j * 2 + 1]));
78             }
79             steps.push([L.latLng(s.startPoint.lat, s.startPoint.lng), d, s.narrative, s.distance * 1000, lineseg]);
80           }
81
82           callback(null, {
83             line: line,
84             steps: steps,
85             distance: data.route.distance * 1000,
86             time: data.route.time
87           });
88         }
89       });
90     }
91   };
92 }
93
94 if (OSM.MAPQUEST_KEY) {
95   OSM.Directions.addEngine(new MapQuestEngine("mapquest_bicycle", "routeType=bicycle"), true);
96   OSM.Directions.addEngine(new MapQuestEngine("mapquest_foot", "routeType=pedestrian"), true);
97   OSM.Directions.addEngine(new MapQuestEngine("mapquest_car", "routeType=fastest"), true);
98 }