forked from hdkw/socialspeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
312 lines (238 loc) · 8.77 KB
/
app.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
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
$(function() {
/**************************************************
*
* Vars
*
**************************************************/
var $inputRange = $('input[type="range"]');
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
// I put duration, distance and specialtrip as general variables because I don't know how to extract them or pass them from a function to another
var distance = 0;
var duration = 0;
var specialtrip = 1;
/**************************************************
*
* Init
*
**************************************************/
// Init google map
google.maps.event.addDomListener(window, 'load', initGoogleMap);
// Init slider
$inputRange.rangeslider({
polyfill: false
});
// Init slider range value
$inputRange.each(function(){
valueOutput($(this));
});
/**************************************************
*
* Functions
*
**************************************************/
/*
*
* GoogleMap function
*
*/
function initGoogleMap(){
var markers = [];
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
// Options
var mapOptions = {
zoom: 7,
center: new google.maps.LatLng(41.850033, -87.6500523)
};
// map initialization
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
// center map on user location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(initialLocation);
});
}
// Create a search box with autocomplete and link it to the UI element.
var searchBox = new google.maps.places.SearchBox((document.getElementById('origin')));
var searchBox2 = new google.maps.places.SearchBox((document.getElementById('destination')));
// Bias the SearchBox results towards places that are within the bounds of the
// current map's viewport.
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
}
/*
*
* GoogleMap route function
*
*/
function googleMapRoute() {
var start = $('#origin').val(),
end = $('#destination').val(),
selectedMode = $('#mode').val();
if (start != "" && end != "" && selectedMode != null) {
if (selectedMode.substr(0, 7) == "DRIVING") {
selectedMode = "DRIVING";
}
var request = {
origin: start ,
destination: end,
travelMode: google.maps.TravelMode[selectedMode]
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
// Calculate distance and duration for the trip
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [start],
destinations: [end],
travelMode: google.maps.TravelMode[selectedMode],
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, googleMapCallback);
}
}
/*
*
* GoogleMap route function
*
*/
function googleMapCallback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
console.log('Error was: ' + status);
} else {
var origins = response.originAddresses,
destinations = response.destinationAddresses,
$outputDiv = $('#outputDiv'),
html = '';
$outputDiv.html('');
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
// display the trip characteristics to check
html += origins[i] + ' to ' + destinations[j]
+ ': ' + results[j].distance.text + ' in '
+ results[j].duration.text;
// values for distance and directions in meters and seconds
distance = results[j].distance.value;
duration = results[j].duration.value;
html += '(' + duration + ')';
}
// update div
$outputDiv.html(html);
}
}
updateResult();
}
/*
*
* Update result function
*
*/
function updateResult() {
var $socialtime = $("#socialtime"),
$socialspeed = $("#socialspeed"),
$cost = $("#cost"),
$wage = $("#wage"),
$costout = $("#costout"),
$wageout = $("#wageout"),
selectedMode = $('#mode').val();
var num1 = parseFloat($cost.val());
var num2 = 1;
// Formula depending on the transport mode
if (selectedMode != null) {
if (selectedMode.substr(0, 7) == "DRIVING") {
waitingTime = 0;
num2 = parseFloat($wage.val());
if (selectedMode == "DRIVING_HITCH") {
specialtrip = 99999;
waitingTime = parseFloat($("#waiting").val())*60;
} else if (selectedMode == "DRIVING_SHARE2") {
specialtrip = 2;
} else if (selectedMode == "DRIVING_SHARE3") {
specialtrip = 3;
} else if (selectedMode == "DRIVING_SHARE4") {
specialtrip = 4;
} else if (selectedMode == "DRIVING_SHARE5") {
specialtrip = 5;
}
//----------------------------------
// formula for the generalized time in minutes. need to convert it to days and hours and minutes..
var num3 = ((duration+waitingTime+((distance*num1)/specialtrip)/num2)/60).toFixed(0);
//----------------------------------
// formula for the generalized speed
var num4 = (distance/(duration+waitingTime+(distance*num1)/specialtrip/num2)*3.6).toFixed(0);
} else if (selectedMode == "WALKING") {
/*
TODO
*/
//----------------------------------
// formula for the generalized time in minutes. need to convert it to days and hours and minutes..
var num3 = ((duration+((distance*num1)/specialtrip)/num2)/60).toFixed(0);
//----------------------------------
// formula for the generalized speed
var num4 = (distance/(duration+(distance*num1)/specialtrip/num2)*3.6).toFixed(0);
} else if (selectedMode == "BICYCLING") {
/*
TODO
*/
//----------------------------------
// formula for the generalized time in minutes. need to convert it to days and hours and minutes..
var num3 = ((duration+((distance*num1)/specialtrip)/num2)/60).toFixed(0);
//----------------------------------
// formula for the generalized speed
var num4 = (distance/(duration+(distance*num1)/specialtrip/num2)*3.6).toFixed(0);
} else if (selectedMode == "TRANSIT") {
/*
TODO
*/
num1 = $("#transitprice").val();
//----------------------------------
// formula for the generalized time in minutes. need to convert it to days and hours and minutes..
var num3 = ((duration+((distance*num1)/specialtrip)/num2)/60).toFixed(0);
//----------------------------------
// formula for the generalized speed
var num4 = (distance/(duration+(distance*num1)/specialtrip/num2)*3.6).toFixed(0);
}
$socialtime.html(num3);
$socialspeed.html(num4);
}
}
/*
*
* Update range value
*
*/
function valueOutput(element) {
var $this = $(element),
$output = $this.parent().find('.output');
// output html update
$output.html($this.val());
}
/**************************************************
*
* Events
*
**************************************************/
$(document).on("change", "#origin, #destination", function(e){
googleMapRoute();
});
$(document).on("change", "#mode", function(e){
googleMapRoute();
$(".transport.option").hide();
$(".transport.option." + $(this).val().toLowerCase()).show();
});
$(document).on("change", 'input[type="range"], .transport.option input', function(e) {
valueOutput(e.target);
updateResult();
});
});