]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/mapzen.js
Merge remote-tracking branch 'openstreetmap/pull/1344'
[rails.git] / app / assets / javascripts / index / directions / mapzen.js
1 function MapzenEngine(id, costing) {
2   var MZ_INSTR_MAP = [
3     0,  // kNone = 0;
4     8, // kStart = 1;
5     8, // kStartRight = 2;
6     8, // kStartLeft = 3;
7     14,  // kDestination = 4;
8     14,  // kDestinationRight = 5;
9     14,  // kDestinationLeft = 6;
10     0,  // kBecomes = 7;
11     0,  // kContinue = 8;
12     1,  // kSlightRight = 9;
13     2,  // kRight = 10;
14     3,  // kSharpRight = 11;
15     4,  // kUturnRight = 12;
16     4,  // kUturnLeft = 13;
17     7,  // kSharpLeft = 14;
18     6,  // kLeft = 15;
19     5,  // kSlightLeft = 16;
20     0,  // kRampStraight = 17;
21     24, // kRampRight = 18;
22     25, // kRampLeft = 19;
23     24, // kExitRight = 20;
24     25, // kExitLeft = 21;
25     0,  // kStayStraight = 22;
26     1,  // kStayRight = 23;
27     5,  // kStayLeft = 24;
28     20, // kMerge = 25;
29     10, // kRoundaboutEnter = 26;
30     11, // kRoundaboutExit = 27;
31     17, // kFerryEnter = 28;
32     0   // kFerryExit = 29;
33   ];
34
35   return {
36     id: id,
37     creditline: "<a href='https://mapzen.com/projects/valhalla' target='_blank'>Mapzen</a>",
38     draggable: false,
39
40     getRoute: function (points, callback) {
41       return $.ajax({
42         url: document.location.protocol + OSM.MAPZEN_VALHALLA_URL,
43         data: {
44           api_key: OSM.MAPZEN_VALHALLA_KEY,
45           json: JSON.stringify({
46             locations: points.map(function (p) { return { lat: p.lat, lon: p.lng }; }),
47             costing: costing,
48             directions_options: {
49               units: "km",
50               language: I18n.currentLocale()
51             }
52           })
53         },
54         dataType: "json",
55         success: function (data) {
56           var trip = data.trip;
57
58           if (trip.status === 0) {
59             var line = [];
60             var steps = [];
61             var distance = 0;
62             var time = 0;
63
64             trip.legs.forEach(function (leg) {
65               var legLine = L.PolylineUtil.decode(leg.shape, {
66                 precision: 6
67               });
68
69               line = line.concat(legLine);
70
71               leg.maneuvers.forEach(function (manoeuvre) {
72                 var point = legLine[manoeuvre.begin_shape_index];
73
74                 steps.push([
75                   { lat: point[0], lng: point[1] },
76                   MZ_INSTR_MAP[manoeuvre.type],
77                   manoeuvre.instruction,
78                   manoeuvre.length * 1000,
79                   []
80                 ]);
81               });
82
83               distance = distance + leg.summary.length;
84               time = time + leg.summary.time;
85             });
86
87             callback(false, {
88               line: line,
89               steps: steps,
90               distance: distance * 1000,
91               time: time
92             });
93           } else {
94             callback(true);
95           }
96         },
97         error: function () {
98           callback(true);
99         }
100       });
101     }
102   };
103 }
104
105 if (OSM.MAPZEN_VALHALLA_KEY) {
106   OSM.Directions.addEngine(new MapzenEngine("mapzen_car", "auto"), true);
107   OSM.Directions.addEngine(new MapzenEngine("mapzen_bicycle", "bicycle"), true);
108   OSM.Directions.addEngine(new MapzenEngine("mapzen_foot", "pedestrian"), true);
109 }