]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/routing_engines/mapquest.js
Merge pull request #14 from tomhughes/routing
[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: 'Directions courtesy of <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         MQ_SPRITE_MAP: {
22             0: 1, // straight
23             1: 2, // slight right
24             2: 3, // right
25             3: 4, // sharp right
26             4: 5, // reverse
27             5: 6, // sharp left
28             6: 7, // left
29             7: 8, // slight left
30             8: 5, // right U-turn
31             9: 5, // left U-turn
32             10: 2, // right merge
33             11: 8, // left merge
34             12: 2, // right on-ramp
35             13: 8, // left on-ramp
36             14: 2, // right off-ramp
37             15: 8, // left off-ramp
38             16: 2, // right fork
39             17: 8, // left fork
40             18: 1  // straight fork
41         },
42         getRoute: function(isFinal,points) {
43             var url="http://open.mapquestapi.com/directions/v2/route?key=Fmjtd%7Cluur290anu%2Crl%3Do5-908a0y";
44             var from=points[0]; var to=points[points.length-1];
45             url+="&from="+from.join(',');
46             url+="&to="+to.join(',');
47             url+="&"+that.vehicleParam;
48             //url+="&locale=" + I18n.currentLocale(); //Doesn't actually work. MapQuest requires full locale e.g. "de_DE", but I18n only provides language, e.g. "de"
49             url+="&manMaps=false";
50             url+="&shapeFormat=raw&generalize=0&unit=k";
51             this.requestCORS(url);
52         },
53         gotRoute: function(router,data) {
54             if (data.info.statuscode!=0) return false;
55         
56             var poly=[];
57             var shape=data.route.shape.shapePoints;
58             for (var i=0; i<shape.length; i+=2) {
59                 poly.push(L.latLng(shape[i],shape[i+1]));
60             }
61             router.setPolyline(poly);
62
63             // data.shape.maneuverIndexes links turns to polyline positions
64             // data.legs[0].maneuvers is list of turns
65             var steps=[];
66             var mq=data.route.legs[0].maneuvers;
67             for (var i=0; i<mq.length; i++) {
68                 var s=mq[i];
69                 var d=(i==mq.length-1) ? 15: this.MQ_SPRITE_MAP[s.turnType];
70                 steps.push([L.latLng(s.startPoint.lat, s.startPoint.lng), d, s.narrative, s.distance*1000]);
71             }
72             router.setItinerary( { steps: steps, distance: data.route.distance*1000, time: data.route['time'] });
73             return true;
74         }
75     };
76 };
77
78 OSM.RoutingEngines.list.push(new MapQuestEngine("Bicycle", "routeType=bicycle").createConfig());
79 OSM.RoutingEngines.list.push(new MapQuestEngine("Foot", "routeType=pedestrian").createConfig());
80 OSM.RoutingEngines.list.push(new MapQuestEngine("Car", "routeType=fastest").createConfig());
81 // can be: routeType=fastest|shortest|pedestrian|multimodal|bicycle