-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
232 lines (179 loc) · 7.17 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Defining Global Variables
var API_key = "NgCAXXvHYck3IBA8phZemfgEtauA8jGU";
var Base_URL = "api.tomtom.com";
function getRadioButton(name) {
let button = document.getElementsByName(name);
for(i = 0; i < button.length; i++) {
if(button[i].checked) {
return button[i].value;
}
}
}
function getRoute(lat1, long1, lat2, long2, storage) {
// Define Variables for API call
let routeType = getRadioButton('routeType');
let hilliness = getRadioButton('hills');
let modeTravel = getRadioButton('modeTravel');
// Store in object
storage.request.routeType = routeType;
storage.request.hilliness = hilliness;
storage.request.modeTravel = modeTravel;
console.log(routeType, hilliness, modeTravel);
let apiCall = "";
if (routeType == "thrilling") {
apiCall = "https://" + Base_URL + "/routing/1/calculateRoute/" + lat1 + "," + long1 + ":" + lat2 + "," + long2 + "/json?instructionsType=text&language=en-US&vehicleHeading=90&§ionType=traffic&report=effectiveSettings&routeType=" + routeType + "&traffic=true&travelMode=" + modeTravel + "&hilliness=" + hilliness + "&key=" + API_key;
} else {
apiCall = "https://" + Base_URL + "/routing/1/calculateRoute/" + lat1 + "," + long1 + ":" + lat2 + "," + long2 + "/json?instructionsType=text&language=en-US&vehicleHeading=90&§ionType=traffic&report=effectiveSettings&routeType=" + routeType + "&traffic=true&travelMode=" + modeTravel + "&key=" + API_key;
}
// Store in object
storage.request.apiCall = apiCall;
// Routing Call
r=$.ajax({
url: apiCall,
method: "GET"
}).done(function(data) {
// Routing data collection
console.log("GET Request Routing", data.routes[0]);
let distanceKilo = Math.round(data.routes[0].summary.lengthInMeters / 1000);
let travelTime = Math.round(data.routes[0].summary.travelTimeInSeconds / 60);
let delayTime = Math.round(data.routes[0].summary.trafficDelayInSeconds / 60);
// Store in object
storage.response.distanceKilo = distanceKilo;
storage.response.travelTime = travelTime;
storage.response.delayTime = delayTime;
// Route Summary
$("#distance").html(distanceKilo);
$("#travelTime").html(travelTime);
$("#trafficDelay").html(delayTime);
console.log("Length of Instruction : ", data.routes[0].guidance.instructions.length);
console.log("Storage Object : ", storage);
var legs = [];
// Looping Through Instructions
for (let i = 0; i <= data.routes[0].guidance.instructions.length-1; i++) {
let message = data.routes[0].guidance.instructions[i].message;
let time = data.routes[0].guidance.instructions[i].travelTimeInSeconds / 60;
time = time.toFixed(1);
let distance = data.routes[0].guidance.instructions[i].routeOffsetInMeters / 1000;
distance = distance.toFixed(2);
let latMap = data.routes[0].guidance.instructions[i].point.latitude;
let longMap = data.routes[0].guidance.instructions[i].point.longitude;
let apiMap = "https://api.tomtom.com/map/1/staticimage?layer=basic&style=main&format=jpg&zoom=12¢er=" + longMap + "," + latMap + "&width=512&height=512&view=Unified&key=" + API_key;
$("#path").append("<li class='list-group-item'>" + (1+i) + ".) " + message + ". | " + time + " min | " + distance + " km");
$("#path").append("<img alt='Map for " + latMap + ", " + longMap + "' class='img-thumbnail rounded mx-auto d-block' src='" + apiMap + "'></li>");
legs.push({"message":message, "time":time, "distance":distance, "map":apiMap});
}
storage.response.legs = legs;
// Send Object
r=$.ajax({
url: "http://172.17.13.127/final.php?method=setMap",
method: "POST",
dataType: "json",
data: JSON.stringify(storage),
contentType: "application/json"
}).done(function(data) {
console.log("return", data);
});
}).fail(function(error) {
// Error in Routing API Call
console.log("Failure in GET Request Routing", error);
});
}
function getLatLong(query1, query2) {
let storage = createJSON();
// Get Lat Long
r=$.ajax({
url: "https://" + Base_URL + "/search/2/geocode/" + query1 + ".json?key=" + API_key,
method: "GET"
}).done(function(data) {
console.log("GET Request for Geocode Location", data.results[0]);
// Get Lat and Long
let lat1 = data.results[0].position.lat;
let long1 = data.results[0].position.lon;
$("#startLatLong").html(lat1 + ", " + long1);
// Store in object
storage.request.lat1 = lat1;
storage.request.long1 = long1;
// Get Lat Long Again
r=$.ajax({
url: "https://" + Base_URL + "/search/2/geocode/" + query2 + ".json?key=" + API_key,
method: "GET"
}).done(function(data) {
console.log("GET Request for Geocode Location", data.results[0]);
// Get Lat and Long
let lat2 = data.results[0].position.lat;
let long2 = data.results[0].position.lon;
$("#endLatLong").html(lat2 + ", " + long2);
// Store in object
storage.request.lat2 = lat2;
storage.request.long2 = long2;
// Routing Call
getRoute(lat1, long1, lat2, long2, storage);
}).fail(function(error) {
// Search Failure
console.log("Failure in GET Request Geocode Query2", error);
$("#errorMessage4").removeClass('hiddenSection');
});
}).fail(function(error) {
// Search Failure
console.log("Failure in GET Request Geocode Query1", error);
$("#errorMessage3").removeClass('hiddenSection');
});
}
function inputValid(input1, input2) {
if ((input1 == "") || (input2 == "")) {
if (input1 == "") {
console.log("Empty Input for Starting Address");
$("#errorMessage1").removeClass('hiddenSection');
}
if (input2 == "") {
console.log("Empty Input for Ending Address");
$("#errorMessage2").removeClass('hiddenSection');
}
return false;
}
return true;
}
function clearErrors() {
$("#errorMessage1").addClass('hiddenSection');
$("#errorMessage2").addClass('hiddenSection');
$("#errorMessage3").addClass('hiddenSection');
$("#errorMessage4").addClass('hiddenSection');
}
function createJSON() {
let storage =
{
"request": {
"apiCall" : "",
"lat1" : 0,
"long1" : 0,
"lat2" : 0,
"long2" : 0,
"routeType" : "",
"hilliness" : "",
"modeTravel" : ""
},
"response": {
"distanceKilo" : 0,
"travelTime" : 0,
"delayTime" : 0
}
};
console.log(storage);
return storage;
}
$(document).ready(function() {
console.log("Document Ready");
$("#submit").click(function() {
console.log("Submit Button Clicked");
// Get Address Information
let start = document.getElementById("startAddress").value;
let end = document.getElementById("endAddress").value;
clearErrors();
if (inputValid(start, end)) {
// Make Visible Results
$("#results").removeClass("hiddenSection");
// Get Results
getLatLong(start, end);
}
});
});