-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
151 lines (122 loc) · 7.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
let startCoords = "";
let endCoords = "";
let startLoc = "";
let endLoc = "";
let hasPublicTransit = false;
let alreadyWarned = false;
$( "#searchButton" ).click(function( event ) {
// Stop form from submitting normally
event.preventDefault();
// Get some values from elements on the page:
var start = encodeURIComponent($("#start").val());
var end = encodeURIComponent($("#end").val());
$.getJSON( "./api/validate.php/?query="+start, ( data ) => {
$("#result").empty().append('<div style="text-align: center;font-size:8em;width:100%;"><i class="fas fa-cog fa-spin"></i></div>');
}).done(function(data){
startCoords = data.coords;
startLoc = data.location_name;
$.getJSON( "./api/validate.php/?query="+end, ( data ) => {
}).done(function(data){
endCoords = data.coords;
endLoc = data.location_name;
$.getJSON( "./api/index.php?start="+startCoords+"&end="+endCoords, function( data ) {
// Put the results in a div
let content = ""
console.log(data);
for (var key in data) {
travelType = "default";
splitStart = startCoords.split(",");
splitEnd = endCoords.split(",");
if(data[key].name.includes("Uber")) {
travelType = "Uber";
data[key].directions = '<a class="btn btn-primary" href="https://m.uber.com/ul/?client_id=<CLIENT_ID>&action=setPickup&pickup[latitude]=' + splitStart[0] + '&pickup[longitude]=' + splitStart[1] + '&dropoff[latitude]=' + splitEnd[0] + '&dropoff[longitude]=' + splitStart[1] + '&product_id=a1111c8c-c720-46c3-8534-2fcdd730040d&link_text=View%20team%20roster&partner_deeplink=partner%3A%2F%2Fteam%2F9383"><i class="fab fa-fw fa-uber"></i> Order Uber</a>';
}
if(data[key].name.includes("Lyft")) {
travelType = "Lyft";
data[key].directions = '<a class="btn btn-primary" href="https://lyft.com/ride?id=lyft&pickup[latitude]=' + splitStart[0] + '&pickup[longitude]=' + splitStart[1] + '&partner=GZA6JsK6N0b9&destination[latitude]=' + splitEnd[0] + '&destination[longitude]=' + splitEnd[1] + '"><i class="fab fa-fw fa-lyft"></i> Order Lyft</a>';
}
if (data[key].name == "Bike" || data[key].name == "Car" || data[key].name == "Walking") {
travelType = data[key].name;
data[key].directions = '<a class="btn btn-primary" href="http://maps.apple.com/?saddr=' + startCoords + '&daddr=' + endCoords + '"><i class="fas fa-fw fa-map-marker-alt"></i> Navigate</a>';
}
if (data[key].name.includes("Transit")) {
travelType = "Transit";
data[key].directions = '<a class="btn btn-primary" href="https://wego.here.com/directions/publicTransport/' + startLoc + '/' + endLoc + '"><i class="fas fa-fw fa-bus"></i> Navigate</a>';
hasPublicTransit = true;
}
if (data[key].name == "Bird" || data[key].name == "Mobike") {
travelType = "Scooter";
data[key].directions = '<a disabled class="btn disabled btn-primary" href="#"><i class="fas fa-fw fa-lock"></i> Unavailable</a>';
}
if (data[key].name.includes("with")) {
travelType = "Hybrid";
data[key].directions = '<a disabled class="btn disabled btn-primary" href="#"><i class="fas fa-fw fa-lock"></i> Unavailable</a>';
}
switch(data[key].currency) {
case "CAD":
currencySymbol = "C$";
break;
case "USD":
currencySymbol = "$";
break
case "GBP":deeplink
currencySymbol = "£";
break;
case "EUR":
currencySymbol = "€";
break;
}
content += "<div class='col-12 col-md-6'><div class='card background-" + travelType + "'><div class ='card-body'>";
if (data.hasOwnProperty(key)) {
let name = data[key].name;
let currency = data[key].currency;
let price = data[key].price;
let distance = data[key].distance;
let time = data[key].time;
let timeInMin = Math.floor(Number(time)/60);
let seconds = Number(time)%60;
if(!currency){
content += "<h5 id = '" +name+"' class = 'card-title'><span class='transport-name'>"+name + "</span> ";
content += "<span class = 'price price-free'>FREE</span></h5>";
content += "<p><i class = 'far fa-fw fa-clock'></i> "+timeInMin+" minutes</p>"
content += "<p><i class = 'fas fa-fw fa-map-marked-alt'></i> "+distance+" miles</p>"
content += data[key].directions
}
else if (data[key].prices_unavailable) {
content += "<h5 id = '" +name+"' class = 'card-title'><span class='transport-name'>"+name + "</span> ";
content += " <span class = 'price price-unavailable' id='" + currency + "'>?</span></h5>";
content += "<p><i class = 'far fa-fw fa-clock'></i> "+timeInMin+" minutes</p>"
content += "<p><i class = 'fas fa-fw fa-map-marked-alt'></i> "+distance+" miles</p>"
content += data[key].directions
} else {
content += "<h5 id = '" +name+"' class = 'card-title'><span class='transport-name'>"+name + "</span> ";
content += " <span class = 'price price-paid' id='" + currency + "'>"+ currencySymbol +price+"</span></h5>";
content += "<p><i class = 'far fa-fw fa-clock'></i> "+timeInMin+" minutes</p>"
content += "<p><i class = 'fas fa-fw fa-map-marked-alt'></i> "+distance+" miles</p>"
content += data[key].directions
}
}
content += "</div></div></div>"
}
if(alreadyWarned == false && hasPublicTransit == false) {
content += "<div class='col-12 col-md-6'><div class='card background-alert'><div class='card-body'>It looks like your area doesn't have reliable public transport. Have you considered <a href='https://callyourrep.co/result/?address=" + startLoc + "'>contacting your local representative?</a></div></div></div>";
}
alreadyWarned = true;
$("#result").empty().append(content);
});
});
// Send the data using post
});
});
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(queryPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
function queryPosition(location) {
$.getJSON('https://reverse.geocoder.api.here.com/6.2/reversegeocode.json?prox=' + location.coords.latitude + ',' + location.coords.longitude + '&mode=retrieveAddresses&maxresults=1&gen=9&app_id=jPeL8FxtgBOBIB9ISZPZ&app_code=mXQv9GX6ULnbMkeJYR2rVQ', (data) => {
}).done(function(data) {
$("#start").val(data['Response']['View'][0]['Result'][0]['Location']['Address']['Label']);
})};
}