-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
290 lines (253 loc) · 9.51 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
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 20%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
table, th, td {
border: 0px solid black;
}
tr:nth-child(even) { background: #CCC};
tr:nth-child(odd) { background: #FFF};
</style>
</head>
<body>
<div id="map"></div>
<div>
<form onsubmit="return false" autocomplete="on">
<input type="text" size=50 id="PropertyOfInterest" name="PropertyOfInterestName">
<input type="submit" onclick="onSubmitClicked()">
</form>
</div>
<div id="time"></div>
<div id="requests"></div>
<!-- <script src="https://maps.googleapis.com/maps/api/js"></script> -->
<script>
var map;
var gMapRequests = [];
var kPending = 0;
var kSuccess = 1;
var kFailure = 2;
var gPropertyOfInterest = "";
function onSubmitClicked() {
console.log("onSubmitClicked");
gPropertyOfInterest = document.getElementById('PropertyOfInterest').value;
console.log("gPropertyOfInterest: " + gPropertyOfInterest);
initMap();
}
function MapRequest() {
this.start = "";
this.end = "";
this.index = 0;
this.mode = google.maps.DirectionsTravelMode.DRIVING;
this.departTime = new Date('2018-10-10T16:00:00');
this.bestGuessTime = "";
this.optimisticTime = "";
this.pessimisticTime = "";
this.status = kPending;
this.errMsg = "";
}
function addRequest( request ) {
request.index = gMapRequests.length;
gMapRequests.push( request );
}
function startRequest( mapRequest, delay ) {
console.log("start request");
addRequest( mapRequest );
var request = {
origin: mapRequest.start,
destination: mapRequest.end,
travelMode: mapRequest.mode,
provideRouteAlternatives: false
};
var isDriving = (mapRequest.mode === google.maps.DirectionsTravelMode.DRIVING);
if( isDriving ) {
request.drivingOptions = {
departureTime: mapRequest.departTime,
trafficModel: google.maps.TrafficModel.BEST_GUESS
}
request.avoidTolls = true;
} else {
request.transitOptions = {
departureTime: mapRequest.departTime
}
}
var reqIndex = mapRequest.index;
var onResponse = function( response, status ) {
if( status === 'OK' &&
gMapRequests[reqIndex].status != kFailure ) {
var point = response.routes[ 0 ].legs[ 0 ];
if( !isDriving ) {
gMapRequests[reqIndex].status = kSuccess;
gMapRequests[reqIndex].bestGuessTime = point.duration.text;
} else {
if( response.request.drivingOptions.trafficModel ===
google.maps.TrafficModel.BEST_GUESS) {
gMapRequests[reqIndex].bestGuessTime = point.duration_in_traffic.text;
} else if( response.request.drivingOptions.trafficModel ===
google.maps.TrafficModel.OPTIMISTIC ) {
gMapRequests[reqIndex].optimisticTime = point.duration_in_traffic.text;
} else if( response.request.drivingOptions.trafficModel ===
google.maps.TrafficModel.PESSIMISTIC ) {
gMapRequests[reqIndex].pessimisticTime = point.duration_in_traffic.text;
gMapRequests[reqIndex].status = kSuccess;
}
}
//console.log(JSON.stringify( response ) );
} else {
gMapRequests[reqIndex].status = kFailure;
gMapRequests[reqIndex].errMsg = status;
console.log("Routing error! Response dump:");
console.log(JSON.stringify( response ) );
}
refreshUI();
}
setTimeout( function() {
var directionsService = new google.maps.DirectionsService();
directionsService.route( request, onResponse );
}, delay );
if( isDriving ) {
setTimeout( function() {
request.drivingOptions.trafficModel =
google.maps.TrafficModel.OPTIMISTIC;
var directionsService = new google.maps.DirectionsService();
directionsService.route( request, onResponse );
}, delay+330 );
setTimeout( function() {
request.drivingOptions.trafficModel =
google.maps.TrafficModel.PESSIMISTIC;
var directionsService = new google.maps.DirectionsService();
directionsService.route( request, onResponse );
}, delay+660 );
}
}
function refreshUI() {
var html = "<table>";
html += "<tr><th>From</th><th>To</th><th>Departing at</th><th>Travel time</th></tr>";
for(i = 0; i < gMapRequests.length; ++i ) {
html += "<tr>";
html += "<td>" + gMapRequests[i].start + "</td>";
html += "<td>" + gMapRequests[i].end + "</td>";
html += "<td>" + gMapRequests[i].departTime.toLocaleString() + "</td>";
if( gMapRequests[i].status === kPending ) {
html += "<td>Calculating</td>";
} else if( gMapRequests[i].status === kSuccess ) {
if( gMapRequests[i].mode === google.maps.DirectionsTravelMode.DRIVING ) {
html += "<td>" + gMapRequests[i].bestGuessTime
+ " (" + gMapRequests[i].optimisticTime + " - "
+ gMapRequests[i].pessimisticTime + ")</td>";
} else {
html += "<td>" + gMapRequests[i].bestGuessTime + "</td>";
}
} else {
html += "<td>Error! " + gMapRequests[i].errMsg + "</td>";
}
}
html += "</table>";
document.getElementById('requests').innerHTML = html;
}
function getTravelDate( hour, min )
{
var travelDate = new Date(); // initialized to today's date.
var offset = [1,1,1,1,1,3,2]; // move to the next nearest workday.
travelDate.setDate( travelDate.getDate() + offset[ travelDate.getDay() ]);
travelDate.setHours( hour );
travelDate.setMinutes( min );
travelDate.setSeconds(0);
travelDate.setMilliseconds(0);
return travelDate;
}
function initMap() {
console.log("initMap: " + gPropertyOfInterest );
// Do nothing if no property specified
if( !gPropertyOfInterest ) {
return;
}
// clear existing results
// TODO: Must cancel requests currently in flight
// or at least invalidate them somehow. otherwise
// they will mess up the table.
gMapRequests = [];
var mrQueue = [];
var work1 = "7120 Hurontario St, Mississauga, ON L5S 1Z8";
var work2 = "101 College St, Toronto, ON M5G 1L7";
var mr = new MapRequest();
mr.start = gPropertyOfInterest;
mr.end = work1;
mr.departTime = getTravelDate(8,0);
mrQueue.push( mr );
// reverse trip
mr = new MapRequest();
mr.start = work1;
mr.end = gPropertyOfInterest;
mr.departTime = getTravelDate(16,30);
mrQueue.push( mr );
mr = new MapRequest();
mr.start = gPropertyOfInterest;
mr.end = work1;
mr.departTime = getTravelDate(7,0);
mrQueue.push( mr );
// reverse trip
mr = new MapRequest();
mr.start = gPropertyOfInterest;
mr.end = work1;
mr.departTime = getTravelDate(15,30);
mrQueue.push( mr );
mr = new MapRequest();
mr.start = gPropertyOfInterest;
mr.end = work2;
mr.mode = google.maps.DirectionsTravelMode.TRANSIT;
mr.departTime = getTravelDate(9,0);
mrQueue.push( mr );
// reverse trip
mr = new MapRequest();
mr.start = work2;
mr.end = gPropertyOfInterest;
mr.mode = google.maps.DirectionsTravelMode.TRANSIT;
mr.departTime = getTravelDate(17,30);
mrQueue.push( mr );
for( i = 0; i < mrQueue.length; ++i ) {
startRequest( mrQueue[i], i * 1000 );
}
refreshUI();
// center the map around the property of interest
var coder = new google.maps.Geocoder();
var coderReq = {
address: gPropertyOfInterest
};
coder.geocode( coderReq, function(results, status) {
if( status === 'OK' ) {
map.setCenter( results[0].geometry.location );
map.setZoom(15);
var markerOpts = {
map: map,
position: results[0].geometry.location
};
var marker = new google.maps.Marker( markerOpts )
}
});
}
function onGoogleMap() {
console.log("onGoogleMap");
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -25.344, lng: 131.036},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD3MaQdE7XUHMs8ibUkSDRJM0CimlaPiXI&callback=onGoogleMap"
async defer></script>
</body>
</html>