]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions/mapzen.js
1b775ba5106ffec7760c207e19261243ad719b11
[rails.git] / app / assets / javascripts / index / directions / mapzen.js
1 function MapzenEngine(id, costing) {
2   var MZ_INSTR_MAP = [
3     1,  // kNone = 0;
4     14, // kStart = 1;
5     14, // kStartRight = 2;
6     14, // kStartLeft = 3;
7     15, // kDestination = 4;
8     15, // kDestinationRight = 5;
9     15, // kDestinationLeft = 6;
10     1,  // kBecomes = 7;
11     1,  // kContinue = 8;
12     2,  // kSlightRight = 9;
13     3,  // kRight = 10;
14     4,  // kSharpRight = 11;
15     5,  // kUturnRight = 12;
16     5,  // kUturnLeft = 13;
17     6,  // kSharpLeft = 14;
18     7,  // kLeft = 15;
19     8,  // kSlightLeft = 16;
20     1,  // kRampStraight = 17;
21     2,  // kRampRight = 18;
22     8,  // kRampLeft = 19;
23     2,  // kExitRight = 20;
24     8,  // kExitLeft = 21;
25     1,  // kStayStraight = 22;
26     2,  // kStayRight = 23;
27     8,  // kStayLeft = 24;
28     1,  // kMerge = 25;
29     11, // kRoundaboutEnter = 26;
30     12, // kRoundaboutExit = 27;
31     18, // kFerryEnter = 28;
32     1   // 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             }
51           })
52         },
53         dataType: "json",
54         success: function (data) {
55           var trip = data.trip;
56
57           if (trip.status === 0) {
58             var line = [];
59             var steps = [];
60             var distance = 0;
61             var time = 0;
62
63             trip.legs.forEach(function (leg) {
64               var legLine = L.PolylineUtil.decode(leg.shape, {
65                 precision: 6
66               });
67
68               line = line.concat(legLine);
69
70               leg.maneuvers.forEach(function (manoeuvre) {
71                 var point = legLine[manoeuvre.begin_shape_index];
72
73                 steps.push([
74                   { lat: point[0], lng: point[1] },
75                   MZ_INSTR_MAP[manoeuvre.type],
76                   manoeuvre.instruction,
77                   manoeuvre.length * 1000,
78                   []
79                 ]);
80               });
81
82               distance = distance + leg.summary.length;
83               time = time + leg.summary.time;
84             });
85
86             callback(false, {
87               line: line,
88               steps: steps,
89               distance: distance * 1000,
90               time: time
91             });
92           } else {
93             callback(true);
94           }
95         },
96         error: function () {
97           callback(true);
98         }
99       });
100     }
101   };
102 }
103
104 if (OSM.MAPZEN_VALHALLA_KEY) {
105   OSM.Directions.addEngine(new MapzenEngine("mapzen_car", "auto"), true);
106   OSM.Directions.addEngine(new MapzenEngine("mapzen_bicycle", "bicycle"), true);
107   OSM.Directions.addEngine(new MapzenEngine("mapzen_foot", "pedestrian"), true);
108 }