-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
435 lines (382 loc) · 13.7 KB
/
index.html
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<!DOCTYPE html>
<html>
<head>
<title>CalcuRouter</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
.controls {
margin-top: 10px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#origin-input,
#destination-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 200px;
}
#origin-input:focus,
#destination-input:focus {
border-color: #4d90fe;
}
#mode-selector {
color: #fff;
background-color: #4d90fe;
margin-left: 12px;
padding: 5px 11px 0px 11px;
}
#mode-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
.ecost {
visibility: hidden;
padding: 5px;
position: fixed;
bottom: 5%;
left: 15%;
margin-top: -100px;
margin-left: -150px;
background-color: #4d90fe;
border-radius: 5px;
text-align: center;
z-index: 11;
display: inline-block;
line-height: normal;
opacity: 0.88;
}
.text {
color: #fff;
font-family: Roboto;
font-size: 18px;
font-weight: 100;
vertical-align: middle;
}
.transitHeading {
font-weight: 400;
min-width: 80px;
padding-left: 3px;
padding-right: 3px;
}
.transitTitle {
font-weight: 400;
}
table {
border-collapse: collapse;
}
table tr:first-child th {
border-top: 0;
}
table tr:last-child td {
border-bottom: 0;
}
table tr td:first-child,
table tr th:first-child {
border-left: 0;
}
table tr td:last-child,
table tr th:last-child {
border-right: 0;
}
td {
border: 1px #80b0fe;
border-style: inset;
padding: 3px;
}
td.div {
padding-left: 5px;
padding-right: 5px;
}
th {
border: 1px #80b0fe;
border-style: inset;
padding: 3px;
border-top: none;
}
.costitem {
vertical-align: middle;
}
.legendItem {
width: 30px;
}
#cost_walking_color {
background-color: #2AFFD3;
}
#cost_transit_color {
background-color: #1AE859;
}
#cost_driving_color {
background-color: #4FFF1C;
}
#cost_bicycling_color {
background-color: #1AB2E8;
}
.flex-center-vertically {
display: flex;
justify-content: center;
flex-direction: column;
}
</style>
</head>
<body>
<input id="origin-input" class="controls" type="text"
placeholder="Enter an origin location">
<input id="destination-input" class="controls" type="text"
placeholder="Enter a destination location">
<div id="popup" class="ecost flex-center-vertically text">
<table>
<tr>
<th></th>
<th class="transitHeading, legendItem"></th>
<th class="transitHeading">Time</th>
<th class="transitHeading">Distance</th>
<th class="transitHeading">Money</th>
<th class="transitHeading">Carbon</th>
</tr>
<tr>
<td class="transitTitle">Driving</td>
<td id="cost_driving_color" class="legendItem"> </td>
<td><div id="cost_driving_time" class="costitem">-</div></td>
<td><div id="cost_driving_distance" class="costitem">-</div></td>
<td><div id="cost_driving_money" class="costitem">-</div></td>
<td><div id="cost_driving_carbon" class="costitem">-</div></td>
</tr>
<tr>
<td class="transitTitle">Transit</td>
<td id="cost_transit_color" class="legendItem"> </td>
<td><div id="cost_transit_time" class="costitem">-</div></td>
<td><div id="cost_transit_distance" class="costitem">-</div></td>
<td><div id="cost_transit_money" class="costitem">-</div></td>
<td><div id="cost_transit_carbon" class="costitem">-</div></td>
</tr>
<td class="transitTitle">Bicycling</td>
<td id="cost_bicycling_color" class="legendItem"> </td>
<td><div id="cost_bicycling_time" class="costitem">-</div></td>
<td><div id="cost_bicycling_distance" class="costitem">-</div></td>
<td><div id="cost_bicycling_money" class="costitem">-</div></td>
<td><div id="cost_bicycling_carbon" class="costitem">-</div></td>
</tr>
<tr>
<td class="transitTitle">Walking</td>
<td id="cost_walking_color" class="legendItem"> </td>
<td><div id="cost_walking_time" class="costitem">-</div></td>
<td><div id="cost_walking_distance" class="costitem">-</div></td>
<td><div id="cost_walking_money" class="costitem">-</div></td>
<td><div id="cost_walking_carbon" class="costitem">-</div></td>
</tr>
</table>
</div>
<div id="map"></div>
<script>
var directionsDisplayWalking;
var directionsDisplayTransit;
var directionsDisplayDriving;
var directionsDisplayBicycling;
function initMap() {
var origin_place_id = null;
var destination_place_id = null;
var travel_mode = google.maps.TravelMode.DRIVING;
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeControl: false,
center: {lat: 45.57, lng: -122.72},
zoom: 13
});
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
var origin_input = document.getElementById('origin-input');
var destination_input = document.getElementById('destination-input');
var modes = document.getElementById('mode-selector');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(origin_input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(destination_input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(modes);
var origin_autocomplete = new google.maps.places.Autocomplete(origin_input);
origin_autocomplete.bindTo('bounds', map);
var destination_autocomplete = new google.maps.places.Autocomplete(destination_input);
destination_autocomplete.bindTo('bounds', map);
// // Sets a listener on a radio button to change the filter type on Places
// // Autocomplete.
// function setupClickListener(id, mode) {
// var radioButton = document.getElementById(id);
// radioButton.addEventListener('click', function() {
// travel_mode = mode;
// });
// }
// setupClickListener('changemode-walking', google.maps.TravelMode.WALKING);
// setupClickListener('changemode-transit', google.maps.TravelMode.TRANSIT);
// setupClickListener('changemode-driving', google.maps.TravelMode.DRIVING);
function expandViewportToFitPlace(map, place) {
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
}
origin_autocomplete.addListener('place_changed', function() {
var place = origin_autocomplete.getPlace();
if (!place.geometry) {
window.alert("Autocomplete's returned place contains no geometry");
return;
}
expandViewportToFitPlace(map, place);
// If the place has a geometry, store its place ID and route if we have
// the other place ID
origin_place_id = place.place_id;
drawDirections(directionsService);
});
destination_autocomplete.addListener('place_changed', function() {
var place = destination_autocomplete.getPlace();
if (!place.geometry) {
window.alert("Autocomplete's returned place contains no geometry");
return;
}
expandViewportToFitPlace(map, place);
destination_place_id = place.place_id;
drawDirections(directionsService);
});
function drawDirections(directionsService) {
if ( ! (origin_place_id && destination_place_id && directionsService) ) return;
var strokeWeight = 7;
var polylineOptionsWalking = {
strokeColor: '#2AFFD3',
strokeOpacity: 0.7,
strokeWeight: strokeWeight
};
var polylineOptionsTransit = {
strokeColor: '#1AE859',
strokeOpacity: 0.7,
strokeWeight: strokeWeight
};
var polylineOptionsDriving = {
strokeColor: '#4FFF1C',
strokeOpacity: 0.7,
strokeWeight: strokeWeight
};
var polylineOptionsBicycling = {
strokeColor: '#1AB2E8',
strokeOpacity: 0.7,
strokeWeight: strokeWeight
};
if ( directionsDisplayWalking ) directionsDisplayWalking.setMap(null);
directionsDisplayWalking = new google.maps.DirectionsRenderer({suppressMarkers: false, polylineOptions: polylineOptionsWalking});
directionsDisplayWalking.setMap(map);
if ( directionsDisplayTransit ) directionsDisplayTransit.setMap(null);
directionsDisplayTransit = new google.maps.DirectionsRenderer({suppressMarkers: false, polylineOptions: polylineOptionsTransit});
directionsDisplayTransit.setMap(map);
if ( directionsDisplayDriving ) directionsDisplayDriving.setMap(null);
directionsDisplayDriving = new google.maps.DirectionsRenderer({suppressMarkers: false, polylineOptions: polylineOptionsDriving});
directionsDisplayDriving.setMap(map);
if ( directionsDisplayBicycling ) directionsDisplayBicycling.setMap(null);
directionsDisplayBicycling = new google.maps.DirectionsRenderer({suppressMarkers: false, polylineOptions: polylineOptionsBicycling});
directionsDisplayBicycling.setMap(map);
route(origin_place_id, destination_place_id, google.maps.TravelMode.WALKING,
directionsService, directionsDisplayWalking);
route(origin_place_id, destination_place_id, google.maps.TravelMode.TRANSIT,
directionsService, directionsDisplayTransit);
route(origin_place_id, destination_place_id, google.maps.TravelMode.DRIVING,
directionsService, directionsDisplayDriving);
route(origin_place_id, destination_place_id, google.maps.TravelMode.BICYCLING,
directionsService, directionsDisplayBicycling);
};
function route(origin_place_id, destination_place_id, travel_mode,
directionsService, directionsDisplay) {
if (!origin_place_id || !destination_place_id) {
return;
}
directionsService.route({
origin: {'placeId': origin_place_id},
destination: {'placeId': destination_place_id},
travelMode: travel_mode
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
document.getElementById("popup").style.visibility = "visible";
populatePopup(response.routes, travel_mode);
} else {
var cells = document.getElementsByClassName('costitem');
for (var index in cells)
{
var entry = cells[index];
entry.innerHTML = '-';
}
}
});
}
function populatePopup(response, travelmode) {
var costPerMile, fare, time, money, distance;
var metersPerMile = 0.000621371;
costPerMile = 0.54;
switch(travelmode) {
case google.maps.TravelMode.DRIVING:
time = response[0].legs[0].duration.text;
money = parseFloat(response[0].legs[0].distance.value * metersPerMile * costPerMile).toFixed(2);
distance = response[0].legs[0].distance.text;
distance2 = response[0].legs[0].distance.value;
document.getElementById("cost_driving_time").innerHTML = time;
document.getElementById("cost_driving_money").innerHTML = "$" + money;
document.getElementById("cost_driving_distance").innerHTML = distance;
document.getElementById("cost_driving_carbon").innerHTML= (distance2 * metersPerMile * 0.411 * 2.2).toFixed(2) + " lbs" ;
break;
case google.maps.TravelMode.TRANSIT:
console.log(response);
if(response[0].fare === undefined)
fare = '-';
else
fare = response[0].fare.text;
time = response[0].legs[0].duration.text;
distance = response[0].legs[0].distance.text;
distance2 = response[0].legs[0].distance.value;
document.getElementById("cost_transit_money").innerHTML = fare;
document.getElementById("cost_transit_time").innerHTML = time;
document.getElementById("cost_transit_distance").innerHTML = distance;
document.getElementById("cost_transit_carbon").innerHTML = (distance2 * metersPerMile * 0.411 * 2.2 / 2.0).toFixed(2) + " lbs";
break;
case google.maps.TravelMode.BICYCLING:
time = response[0].legs[0].duration.text;
distance = response[0].legs[0].distance.text;
distance2 = response[0].legs[0].distance.value;
document.getElementById("cost_bicycling_money").innerHTML = "$0.00";
document.getElementById("cost_bicycling_time").innerHTML = time;
document.getElementById("cost_bicycling_distance").innerHTML = distance;
document.getElementById("cost_bicycling_carbon").innerHTML= (distance2 * metersPerMile * 0.050 * 2.2).toFixed(2) + " lbs" ;
break;
case google.maps.TravelMode.WALKING:
time = response[0].legs[0].duration.text; // miles and mins
distance = response[0].legs[0].distance.text; // miles
distance2 = response[0].legs[0].distance.value;
document.getElementById("cost_walking_money").innerHTML = "$0.00";
document.getElementById("cost_walking_time").innerHTML = time;
document.getElementById("cost_walking_distance").innerHTML = distance;
document.getElementById("cost_walking_carbon").innerHTML= (distance2 * metersPerMile * 1.0 / 7.0).toFixed(2) + " lbs" ;
break;
default:
//console.log(response[0].legs[0]);
}
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDiQd8NqaSESBmlH1ycGn4nFhijVsI9ykg&libraries=places&signed_in=true&callback=initMap" async defer>
</script>
</body>
</html>