-
Notifications
You must be signed in to change notification settings - Fork 22
/
fts_commute_visualizer.html
1604 lines (1350 loc) · 74.2 KB
/
fts_commute_visualizer.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
LICENSE
This software is dual-licensed under the MIT License and the public domain Unlicense.
See bottom for details.
AUTHOR
Forrest Smith
LINKS
Author: forrestthewoods.xyz
Blog: blog.forrestthewoods.com/visualizing-commute-times-378009330ffa
GitHub: github.com/forrestthewoods/fts_commute_visualizer
-->
<!DOCTYPE html>
<html>
<head>
<title>Forrestthewoods's Commute Visualizer</title>
<link href="https://fonts.googleapis.com/css?family=Karla:400,700" rel="stylesheet">
<!-- This is not an accessible implementation -->
<style>
* {
box-sizing: border-box;
margin: 0;
webkit-font-smoothing: subpixel-antialiased !important;
}
body{
color: #38424E;
font-family: 'Karla', sans-serif;
font-size: 16px;
}
a {
color: #0774C8;
}
a:hover{
color: #2196F0;
}
/* LAYOUT */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#right-panel {
overflow: auto;
float: right;
width: 48vw;
height: 100vh;
padding: 0 0 3rem 0;
}
#map {
height: 100%;
width: 49vw;
border-right: 1px solid #C3D0D6;
}
@media (min-width: 650px){
#right-panel{
width: 49vw;
padding: 1rem 1.5rem 3rem .5rem;
}
}
@media (min-width: 1000px){
#right-panel {
width: 38vw;
padding-left: 0.25rem;
}
#map {
width: 60vw;
}
}
#right-panel h1{
margin: 0 0 0.65rem 0;
}
#right-panel h4{
margin-bottom: 0.3rem;
}
#right-panel .header{
margin-bottom: 3rem;
}
#right-panel .blocks{
margin-bottom: 1.5rem;
}
#right-panel .extra_space{
margin-bottom: 3rem;
}
#right-panel .block_advSearch_1{
margin-bottom: 0.5rem;
}
#right-panel .block_advSearch-visible, #right-panel .block_advSearch-hidden{
padding: 1rem;
border: 1px solid #E6EBEC;
background: #F9FBFC;
}
#right-panel .block_advSearch-visible{
display: block;
}
#right-panel .block_advSearch-hidden{
display: none;
}
#right-panel h1{
font-size: 2.25rem;
font-weight: 700;
line-height: 1;
letter-spacing: 0.03em;
text-transform: uppercase;
color: #2F435E;
}
#right-panel .tinyheader{
text-transform: uppercase;
font-size: 1rem;
font-weight: 400;
margin-bottom: -0.25rem;
letter-spacing: .05em;
display: block;
}
#right-panel .link_text {
text-transform: uppercase;
letter-spacing: .05em;
}
#right-panel h3{
text-transform: uppercase;
font-size: 1rem;
letter-spacing: 0.03rem;
}
#right-panel h4{
text-transform: uppercase;
font-size: 0.75rem;
letter-spacing: 0.03rem;
}
#right-panel label{
margin-left: 0.5rem;
}
#right-panel input#gridSizeField, #right-panel input#searchRadiusField{
width: 7.5rem;
}
#right-panel select, #right-panel input {
font-size: 15px;
padding: 0.4rem;
}
#right-panel ul li{
list-style: none;
display: inline-block;
margin: 0;
}
#right-panel ul.radioOptions {
margin: 0;
padding: 0;
}
#right-panel .radioOptions li{
margin: 0.5rem .5rem 0.5rem 0;
position: relative;
}
#right-panel .radioOptions label, #right-panel .radioOptions input{
margin-left: 0;
}
#right-panel .radioOptions input[type="radio"], input[type="checkbox"]#specifyTavelTime {
position: absolute;
top: 0;
left: 10px;
opacity: 0.001;
z-index: 100;
}
#right-panel .radioOptions label{
z-index: 90;
padding: .3rem .5rem;
background: #F0F6F9;
border: 1px solid #D8E0E4;
border-radius: 0.15rem;
box-shadow: inset 0 1px 0 0 #FFF;
color: #42525E;
font-size: 0.8rem;
letter-spacing: 0.03rem;
text-transform: uppercase;
transition: all .15s ease-in;
}
#right-panel .radioOptions label:focus{
outline: 2px auto #E59700;
outline: 5px auto -webkit-focus-ring-color;
}
#right-panel .radioOptions label:hover {
background: #2196F0;
border: 1px solid #0C76C8;
box-shadow: inset 0 1px 0 0 #5DB4F7;
color: #fff;
transition: all .15s ease-in;
}
#right-panel .radioOptions input[type="radio"]:checked + label{
background: #0774C8;
border: 1px solid #015697;
box-shadow: none;
color: #fff;
}
#right-panel #transitModesDiv div:first-child{
margin-bottom: .3rem;
}
#right-panel .advOpt_enabled, #right-panel .advOpt_disabled, #right-panel .advOpt_Open{
margin-left: 0;
padding: 0.2rem;
letter-spacing: 0.03rem;
text-transform: uppercase;
}
#right-panel .advOpt_enabled, #right-panel .advOpt_Open{
color: #0774C8;
text-decoration: underline;
transition: all .15s ease-in;
}
#right-panel .advOpt_enabled:hover, #right-panel .advOpt_Open:hover{
color: #2196F0;
transition: all .15s ease-in;
}
#right-panel .advOpt_disabled{
color: #ccc;
opacity: 0.45;
transition: all .15s ease-in;
}
#right-panel .advOpt_enabled::before, #right-panel .advOpt_disabled::before, #right-panel .advOpt_Open::before {
display: inline-block;
margin-right: 0.25rem;
}
#right-panel .advOpt_enabled::before, #right-panel .advOpt_disabled::before {
content: '+';
}
#right-panel .advOpt_Open::before{
content: '-';
}
#right-panel #travelTimeDiv{
padding-bottom: .5rem;
}
#right-panel label.option_disabled{
opacity: 0.35;
}
#right-panel label.option_disabled:hover{
background: #F0F6F9;
border: 1px solid #D8E0E4;
box-shadow: inset 0 1px 0 0 #FFF;
color: #42525E;
transition: all .15s ease-in;
}
#right-panel #travelTimeDiv div:last-of-type{
margin-bottom: 1rem;
}
#right-panel i {
font-size: 12px;
}
#right-panel .btn_pos, #right-panel .btn_neg{
padding: 0.5rem .65rem;
font-size: 0.8rem;
letter-spacing: 0.03rem;
text-transform: uppercase;
transition: all .15s ease-in;
}
#right-panel .btn_pos{
background: #00B582;
border: 1px solid #028269;
border-radius: 0.15rem;
box-shadow: inset 0 1px 0 0 #58C8A8;
color: #fff;
font-size: 0.8rem;
letter-spacing: 0.03rem;
text-transform: uppercase;
}
#right-panel .btn_pos:hover{
background: #008C84;
border: 1px solid #006061;
box-shadow: inset 0 1px 0 0 #50AAA5;
transition: all .15s ease-in;
}
#right-panel .btn_neg{
border: none;
background: none;
color: #E53935;
}
#right-panel .btn_neg:hover{
color: #B71C1C;
text-decoration: underline;
transition: all .15s ease-in;
}
#output {
font-size: 11px;
}
/* SEARCH BAR */
#pac-input {
position: absolute;
top: 1.5vh;
left: 20vw;
z-index: 1000!important;
width: 25vw;
padding: 6px 10px;
background-color: #fff;
border: 1px solid #C8CFD2;
border-radius: 0.15rem;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
font-size: 13px;
text-overflow: ellipsis;
}
#pac-input:focus {
border-color: #4d90fe;
}
</style>
</head>
<body>
<div id="right-panel">
<div class="header">
<h1><span class="tinyheader">Forrestthewoods</span>Commute Visualizer</h1>
<a href="http://blog.forrestthewoods.com/visualizing_commute_times" class="link_text">Blog Post</a> // <a href="https://github.com/forrestthewoods/fts_commute_calculator" class="link_text">Source Code</a> // <a href="http://forrestthewoods.xyz/" class="link_text">About Author</a>
</div>
<div class="blocks">
<h3>Travel Mode</h3>
<form id="travelModeForm" onchange="refreshDivs()">
<ul class="radioOptions">
<li><input type="radio" name="travelModeRadio" id="WALKING" value="WALKING"><label for="WALKING">Walking</label>
</li>
<li><input type="radio" name="travelModeRadio" id="BICYCLING" value="BICYCLING"><label for="BICYCLING">Bicycling</label></li>
<li><input type="radio" name="travelModeRadio" id="TRANSIT" value="TRANSIT"><label for="TRANSIT">Transit</label></li>
<li><input type="radio" name="travelModeRadio" id="DRIVING" value="DRIVING" checked><label for="DRIVING">Driving</label></li>
</ul>
</form>
</div>
<div class="blocks">
<h3>Direction</h3>
<form id="typeForm">
<ul class="radioOptions">
<li><input type="radio" name="typeRadio" id="departFrom" value="departFrom" checked><label for="departFrom">Leave from</label></li>
<li><input type="radio" name="typeRadio" id="arriveAt" value="arriveAt" ><label for="arriveAt">Go to</label></li>
</ul>
</form>
</div>
<div class="blocks extra_space">
<div class="block_advSearch_1">
<input type="checkbox" id="specifyTavelTime" value="specifyTravelTime" onchange="refreshDivs()"><label for="specifyTavelTime" class="advOpt_enabled" id="advOptionCheck">Advanced Search</label>
</div>
<div class="block_advSearch block_advSearch-hidden" id="advSearchOptions">
<div id="travelTimeDiv" style="display:none">
<div id="trafficModelDiv" class="blocks" style="display:none">
<h4>Traffic Model</h4>
<form id="trafficModelForm" onchange="refreshURL()">
<ul class="radioOptions">
<li><input type="radio" name="trafficModelRadio" id="bestguess" value=bestguess checked><label for="bestguess">Best Guess</label></li>
<li><input type="radio" name="trafficModelRadio" id="optimistic" value=optimistic><label for="optimistic">Optimistic</label></li>
<li><input type="radio" name="trafficModelRadio" id="pessimistic" value=pessimistic><label for="pessimistic">Pessimistic</label></li>
</ul>
</form>
</div>
<div id="transitModesDiv" class="blocks" style="display:none">
<h4>Preferred mode</h4>
<form id="transitModeForm">
<div><input type="checkbox" name="transitModeCheck" id="BUS" value="BUS" checked><label for="BUS">Bus</label></div>
<div><input type="checkbox" name="transitModeCheck" id="RAIL" value="RAIL" checked><label for="RAIL">Rail</label></div>
</form>
</div>
<div class="blocks">
<h4>Time</h4>
<form id="timeTypeForm">
<ul class="radioOptions">
<li><input type="radio" name="timeTypeRadio" id="leaveAt" value="leaveAt" checked><label for="leaveAt">Leave At</label></li>
<li><input type="radio" name="timeTypeRadio" id="arriveBy" value="arriveBy"><label for="arriveBy" id="arriveByLabel">Arrive By</label></li>
</ul>
</form>
</div>
<select id="travelTimeHoursSelect">
<option value="12:00 am">12:00 am</option> <option value="12:15 am">12:15 am</option> <option value="12:30 am">12:30 am</option> <option value="12:45 am">12:45 am</option>
<option value="1:00 am">1:00 am</option> <option value="1:15 am">1:15 am</option> <option value="1:30 am">1:30 am</option> <option value="1:45 am">1:45 am</option>
<option value="2:00 am">2:00 am</option> <option value="2:15 am">2:15 am</option> <option value="2:30 am">2:30 am</option> <option value="2:45 am">2:45 am</option>
<option value="3:00 am">3:00 am</option> <option value="3:15 am">3:15 am</option> <option value="3:30 am">3:30 am</option> <option value="3:45 am">3:45 am</option>
<option value="4:00 am">4:00 am</option> <option value="4:15 am">4:15 am</option> <option value="4:30 am">4:30 am</option> <option value="4:45 am">4:45 am</option>
<option value="5:00 am">5:00 am</option> <option value="5:15 am">5:15 am</option> <option value="5:30 am">5:30 am</option> <option value="5:45 am">5:45 am</option>
<option value="6:00 am">6:00 am</option> <option value="6:15 am">6:15 am</option> <option value="6:30 am">6:30 am</option> <option value="6:45 am">6:45 am</option>
<option value="7:00 am">7:00 am</option> <option value="7:15 am">7:15 am</option> <option value="7:30 am">7:30 am</option> <option value="7:45 am">7:45 am</option>
<option value="8:00 am">8:00 am</option> <option value="8:15 am">8:15 am</option> <option value="8:30 am" selected="selected">8:30 am</option> <option value="8:45 am">8:45 am</option>
<option value="9:00 am">9:00 am</option> <option value="9:15 am">9:15 am</option> <option value="9:30 am">9:30 am</option> <option value="9:45 am">9:45 am</option>
<option value="10:00 am">10:00 am</option> <option value="10:15 am">10:15 am</option> <option value="10:30 am">10:30 am</option> <option value="10:45 am">10:45 am</option>
<option value="11:00 am">11:00 am</option> <option value="11:15 am">11:15 am</option> <option value="11:30 am">11:30 am</option> <option value="11:45 am">11:45 am</option>
<option value="12:00 pm">12:00 pm</option> <option value="12:15 pm">12:15 pm</option> <option value="12:30 pm">12:30 pm</option> <option value="12:45 pm">12:45 pm</option>
<option value="1:00 pm">1:00 pm</option> <option value="1:15 pm">1:15 pm</option> <option value="1:30 pm">1:30 pm</option> <option value="1:45 pm">1:45 pm</option>
<option value="2:00 pm">2:00 pm</option> <option value="2:15 pm">2:15 pm</option> <option value="2:30 pm">2:30 pm</option> <option value="2:45 pm">2:45 pm</option>
<option value="3:00 pm">3:00 pm</option> <option value="3:15 pm">3:15 pm</option> <option value="3:30 pm">3:30 pm</option> <option value="3:45 pm">3:45 pm</option>
<option value="4:00 pm">4:00 pm</option> <option value="4:15 pm">4:15 pm</option> <option value="4:30 pm">4:30 pm</option> <option value="4:45 pm">4:45 pm</option>
<option value="5:00 pm">5:00 pm</option> <option value="5:15 pm">5:15 pm</option> <option value="5:30 pm">5:30 pm</option> <option value="5:45 pm">5:45 pm</option>
<option value="6:00 pm">6:00 pm</option> <option value="6:15 pm">6:15 pm</option> <option value="6:30 pm">6:30 pm</option> <option value="6:45 pm">6:45 pm</option>
<option value="7:00 pm">7:00 pm</option> <option value="7:15 pm">7:15 pm</option> <option value="7:30 pm">7:30 pm</option> <option value="7:45 pm">7:45 pm</option>
<option value="8:00 pm">8:00 pm</option> <option value="8:15 pm">8:15 pm</option> <option value="8:30 pm">8:30 pm</option> <option value="8:45 pm">8:45 pm</option>
<option value="9:00 pm">9:00 pm</option> <option value="9:15 pm">9:15 pm</option> <option value="9:30 pm">9:30 pm</option> <option value="9:45 pm">9:45 pm</option>
<option value="10:00 pm">10:00 pm</option> <option value="10:15 pm">10:15 pm</option> <option value="10:30 pm">10:30 pm</option> <option value="10:45 pm">10:45 pm</option>
<option value="11:00 pm">11:00 pm</option> <option value="11:15 pm">11:15 pm</option> <option value="11:30 pm">11:30 pm</option> <option value="11:45 pm">11:45 pm</option>
</select>
<select id="travelTimeMonthSelect" onchange="onTravelTimeMonthChanged()">
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select id="travelTimeDaySelect">
<option value="1">1</option> <option value="2">2</option> <option value="3">3</option>
<option value="4">4</option> <option value="5">5</option> <option value="6">6</option>
<option value="7">7</option> <option value="8">8</option> <option value="9">9</option>
<option value="10">10</option> <option value="11">11</option> <option value="12">12</option>
<option value="13">13</option> <option value="14">14</option> <option value="15">15</option>
<option value="16">16</option> <option value="17">17</option> <option value="18">18</option>
<option value="19">19</option> <option value="20">20</option> <option value="21">21</option>
<option value="22">22</option> <option value="23">23</option> <option value="24">24</option>
<option value="25">25</option> <option value="26">26</option> <option value="27">27</option>
<option value="28">28</option> <option value="29">29</option> <option value="30">30</option>
<option value="31">31</option>
</select>
<select id="travelTimeYearSelect">
<option value=2017>2017</option> <option value=2018>2018</option> <option value=2019>2019</option>
</select>
</div>
</div>
</div>
<div class="blocks">
<h4>Hexagon Radius (in meters)</h4>
<input id="gridSizeField" name="gridSizeInput" type="number" value="250">
</div>
<div class="btn_right_align">
<button onclick="Clear()" class="btn_neg">Clear</button>
<button onclick="Calculate()" class="btn_pos">Visualize Commute</button>
</div>
<br>
<br>
<div id="styleOptionsDiv" style="display:none">
<hr>
<br>
<h3>Styling</h3>
<b>Colors:</b>
<input id="polyColorField0" type="string" value="#388C04" onchange="refreshPolyStyle()">
<input id="polyColorField1" type="string" value="#F0DE1B" onchange="refreshPolyStyle()">
<input id="polyColorField2" type="string" value="#FF7215" onchange="refreshPolyStyle()">
<input id="polyColorField3" type="string" value="#E00300" onchange="refreshPolyStyle()">
<br>
<b>Fill Opacity: </b><input id="polyFillOpacity" type="number" value=".5" onchange="refreshPolyStyle()">
<b>Border Opacity: </b><input id="polyStrokeOpacity" type="number" value=".8" onchange="refreshPolyStyle()">
<br><input type="checkbox" id="showMarkerCheck" onchange="refreshMarker()" checked>Show Marker
<br><button onclick="toggleMapUI()">Toggle Map UI</button>
</div>
</div>
<input id="pac-input" class="controls" type="text" placeholder="Search">
<div id="map"></div>
<script>
// IMPORTANT: Replace this key with your own.
// Then scroll to bottom and replace key in async defer script load
var API_KEY = "YOUR_API_KEY";
// Constants
var targetLimit = 10000;
var requestInterval = 1100;
var seattle = { lat:47.60621, lng:-122.332071 };
// Global variables
var map; // google.maps.Map object
var bounds; // LatLng bounds for map
var service; // distanceMatrixService for queries
var markersArray = []; // display markers
var polyArray = []; // search result polygons
var targetLocation; // LatLng of "primary" target location
var targetLocationMarker; // MapMarker for targetLocation
var polyInfoWindow; // popup when hovering over result polygon
var polyInfoWindowSource = -1; // popup source index
var staticMapWidth = 640; // dimensions of static map used for masking water/highways
var staticMapHeight = 640;
var staticMapCanvas; // context object for "drawing" static map so it can be sampled
var queryTimeout = null; // JavaScript timeout object for stepping queries over time
var gridRadius; // size of grid element. hexagon edge size and circumscribed circle radius
var gridInradius; // radius of circle inscribed in a hexagon
var queryIndices = []; // indices used for query. query repeated if rate limiter hit
var rateLimitCounter = 0; // counter rate limit errors so query delay can be dynamically adjusted
var calculateCounter = 0; // counter so old responses can be ignored
var drawing = false; // flag for bounds drawings
var polyFillOpacity = .5;
var polyStrokeOpacity = .8;
var drawPolyline;
// green, yellow, orange, red, purple, blue, grey
var polyColors = [ '#388C04', '#F0DE1B', '#FF7215', '#E00300', /*'#CB55E3', '#3B74ED',*/ '#D3D3D3'];
var travelTimeThresholds = [ 15, 30, 45, 60, /*75, 90,*/ Infinity ];
var targets = []; // array of {polyCenter:LatLng, dest:LatLng} for hexagon grid
var queryOrigins = []; // if departFrom then [targetLocation] else targets
var queryDestinations = []; // if departFrom then targets else [targetLocation]
// HTML Objects
var gridSizeField;
var searchRadiusField;
var typeRadios;
var travelModeRadios;
var trafficModelRadios;
var transitModeChecks;
var travelThresholdFields;
var advancedSearchCheck;
var travelTimeDaySelect;
var travelTimeYearSelect;
var timeTypeRadios;
var showMarkerCheck;
var controlDiv;
// Marker icons
var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';
var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';
// Google Maps API magic numbers
var MERCATOR_RANGE = 256; // Google Maps API magic number
var HALF_MERCATOR_RANGE = MERCATOR_RANGE / 2;
var pixelsPerLonDegree = MERCATOR_RANGE / 360;
var pixelsPerLonRadian = MERCATOR_RANGE / (2 * Math.PI);
var destinationLimit = 25;
// Form accesors
function getType() {
for (var i = 0; i < typeRadios.length; i++)
if (typeRadios[i].checked)
return typeRadios[i].value;
}
function getTravelMode() {
for (var i = 0; i < travelModeRadios.length; i++)
if (travelModeRadios[i].checked)
return travelModeRadios[i].value;
}
function getTimeType() {
for (var i = 0; i < timeTypeRadios.length; i++)
if (timeTypeRadios[i].checked)
return timeTypeRadios[i].value;
}
function getTrafficModel() {
for (var i = 0; i < trafficModelRadios.length; i++) {
if (trafficModelRadios[i].checked) {
var value = trafficModelRadios[i].value;
if (value == 'bestguess') return google.maps.TrafficModel.BEST_GUESS;
if (value == 'optimistic') return google.maps.TrafficModel.OPTIMISTIC;
if (value == 'pessimistic') return google.maps.TrafficModel.PESSIMISTIC;
}
}
}
function getTransitModes() {
var result = [];
for (var i = 0; i < transitModeChecks.length; i++)
if (transitModeChecks[i].checked)
result.push(transitModeChecks[i].value);
return result;
}
function getGridRadius() {
return Number(gridSizeField.value);
}
function getSearchRadius() {
return getGridRadius() * 10;
}
function getTravelTimeThresholdIndex(travelTimeSeconds) {
for (var i = 0; i < travelTimeThresholds.length; ++i)
if (travelTimeSeconds < travelTimeThresholds[i] * 60)
return i;
}
function setTargetLocation(newTarget) {
targetLocation = newTarget;
refreshMarker();
refreshURL();
}
// UI callbacks
function onTravelTimeMonthChanged() {
var limits = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var limit = limits[travelTimeMonthSelect.selectedIndex];
if (travelTimeDaySelect.selectedIndex > limit - 1)
travelTimeDaySelect.selectedIndex = limit - 1;
for (var i = 0; i < 31; ++i)
travelTimeDaySelect.options[i].disabled = i >= limit;
}
// $$$ FOR LIZ
function refreshDivs() {
var isDriving = getTravelMode() === 'DRIVING';
var isTransit = getTravelMode() === 'TRANSIT';
if (!isDriving && !isTransit) {
advancedSearchCheck.checked = false;
}
var advancedSearch = advancedSearchCheck.checked;
if (!isDriving && !isTransit){
document.getElementById("advOptionCheck").classList.remove('advOpt_Open');
document.getElementById("advOptionCheck").classList.remove('advOpt_enabled');
document.getElementById("advOptionCheck").classList.add('advOpt_disabled');
// for making box not visible
document.getElementById("advSearchOptions").classList.remove('block_advSearch-visible');
document.getElementById("advSearchOptions").classList.add('block_advSearch-hidden');
}
else {
document.getElementById("advOptionCheck").classList.remove('advOpt_disabled');
document.getElementById("advOptionCheck").classList.add('advOpt_enabled');
if (advancedSearch){
document.getElementById("advOptionCheck").classList.remove('advOpt_enabled');
document.getElementById("advOptionCheck").classList.add('advOpt_Open');
// for making box visible
document.getElementById("advSearchOptions").classList.remove('block_advSearch-hidden');
document.getElementById("advSearchOptions").classList.add('block_advSearch-visible');
}
else{
document.getElementById("advOptionCheck").classList.remove('advOpt_Open');
document.getElementById("advOptionCheck").classList.add('advOpt_enabled');
// for making box not visible
document.getElementById("advSearchOptions").classList.remove('block_advSearch-visible');
document.getElementById("advSearchOptions").classList.add('block_advSearch-hidden');
}
}
if (isDriving && advancedSearch) {
timeTypeRadios[0].checked = true;
// for liz
// arriveby for cars has to be disabled here, it's forced, this needs styling
timeTypeRadios[1].disabled = true;
document.getElementById("arriveByLabel").classList.add('option_disabled');
}
else if (isTransit && advancedSearch) {
timeTypeRadios[1].disabled = false;
document.getElementById("arriveByLabel").classList.remove('option_disabled');
}
document.getElementById('trafficModelDiv').style.display = isDriving && advancedSearch ? 'block' : 'none';
document.getElementById('transitModesDiv').style.display = isTransit ? 'block' : 'none';
document.getElementById('travelTimeDiv').style.display = advancedSearch ? 'block' : 'none';
refreshURL();
}
function refreshURL() {
var str = '';
// Search Type
str += 'type=' + getType();
// Travel Mode
var _travelMode = getTravelMode();
str += "&travelMode=" + _travelMode;
str += "&gridSize=" + getGridRadius();
str += "&targetLocation=" + targetLocation.lat + "," + targetLocation.lng;
var advancedSearch = specifyTavelTime.checked;
if (advancedSearch) {
str += "&travelTime=" + travelTimeHoursSelect.selectedIndex;
str += "&travelMonth=" + travelTimeMonthSelect.selectedIndex;
str += "&travelDay=" + travelTimeDaySelect.selectedIndex;
// Traffic Model (options)
if (_travelMode === 'DRIVING') {
str += "&trafficModel=" + getTrafficModel();
}
// TransitMode (optional)
if (_travelMode === 'TRANSIT') {
str += "&timeType=" + getTimeType();
str += "&transitMode=";
var modes = getTransitModes();
for (var i = 0; i < modes.length; ++i) {
str = str + modes[i];
if (i != modes.length - 1)
str = str + ","
}
}
}
window.location.hash = str;
}
function refreshPolyStyle() {
polyColors[0] = document.getElementById("polyColorField0").value;
polyColors[1] = document.getElementById("polyColorField1").value;
polyColors[2] = document.getElementById("polyColorField2").value;
polyColors[3] = document.getElementById("polyColorField3").value;
polyFillOpacity = Number(document.getElementById("polyFillOpacity").value);
polyStrokeOpacity = Number(document.getElementById("polyStrokeOpacity").value);
for (var i = 0; i < polyArray.length; ++i) {
var p = polyArray[i];
var c = polyColors[Math.abs(p.zIndex)];
p.setOptions( {
fillColor:c,
fillOpacity:polyFillOpacity,
strokeColor:c,
strokeOpacity:polyStrokeOpacity,
} );
}
}
function refreshMarker() {
if (showMarkerCheck.checked) {
if (targetLocationMarker)
targetLocationMarker.setMap(null);
targetLocationMarker = new google.maps.Marker({
map: map,
position: targetLocation,
clickable: false
});
}
else {
if (targetLocationMarker)
targetLocationMarker.setMap(null);
}
}
function toggleMapUI() {
var disabled = !map.disableDefaultUI;
map.setOptions({ disableDefaultUI: disabled });
document.getElementById('pac-input').style.display = disabled ? 'none' : 'block';
controlDiv.style.display = disabled ? 'none' : 'block';
showMarkerCheck.checked = !disabled;
refreshMarker();
}
// Math Utilities
function toRadians(degrees) { return degrees / 180 * Math.PI; };
function toDegrees(radians) { return radians / Math.PI * 180; };
function computeOffset(location, bearing, distance) {
var point = new google.maps.LatLng(location.lat, location.lng);
var dest = google.maps.geometry.spherical.computeOffset(point, distance, bearing);
return latLngToLiteral(dest);
};
function computeDistance(latLngA, latLngB) {
var a = new google.maps.LatLng(latLngA.lat, latLngA.lng);
var b = new google.maps.LatLng(latLngB.lat, latLngB.lng);
return google.maps.geometry.spherical.computeDistanceBetween(a, b);
}
function latLngToLiteral(latlng) {
return { lat:latlng.lat(), lng:latlng.lng() };
}
function pointInPolygon(pointLatLng, polygonPoints) {
// Intersect ray going out from point against all polygon edges.
// Even number of intersections means outside, odd means inside
function diff(a, b) { return [a[0]-b[0], a[1]-b[1]]; };
function dot(a, b) { return a[0]*b[0] + a[1]*b[1]; };
function cross(a,b) { return a[0]*b[1] - a[1]*b[0]; };
var point = [pointLatLng.lat, pointLatLng.lng];
var count = 0;
for (var i = 0; i < polygonPoints.length; ++i) {
var j = i+1 < polygonPoints.length ? i+1 : 0;
var polyA = polygonPoints[i];
var polyB = polygonPoints[j];
polyA = [polyA.lat(), polyA.lng()];
polyB = [polyB.lat(), polyB.lng()];
var v1 = diff(point, polyA);
var v2 = diff(polyB, polyA);
var v3 = [0, 1];
var d = dot(v2, v3);
if (Math.abs(d) < .00001)
continue;
var t1 = cross(v2,v1) / d;
var t2 = dot(v1,v3) / d;
if (t1 >= 0 && t2 >= 0 && t2 <= 1)
++count;
}
return count % 2 == 1;
}
// Called after Google Maps API script is asynchronously loaded
function initialize() {
targetLocation = seattle;
// Get document elements
gridSizeField = document.getElementById('gridSizeField');
typeRadios = document.getElementById('typeForm').elements;
advancedSearchCheck = document.getElementById('specifyTavelTime');
travelTimeDaySelect = document.getElementById('travelTimeDaySelect');
travelTimeMonthSelect = document.getElementById('travelTimeMonthSelect');
travelTimeYearSelect = document.getElementById('travelTimeYearSelect');
travelModeRadios = document.getElementById('travelModeForm').elements;
trafficModelRadios = document.getElementById('trafficModelForm').elements;
transitModeChecks = document.getElementById('transitModeForm').elements;
timeTypeRadios = document.getElementById('timeTypeForm').elements;
showMarkerCheck = document.getElementById('showMarkerCheck');
travelThresholdFields = [
document.getElementById('travelThreshold0'),
document.getElementById('travelThreshold1'),
document.getElementById('travelThreshold2'),
document.getElementById('travelThreshold3'),
];
// Initialize canvas
var canvas = document.createElement('canvas');
canvas.setAttribute('width', staticMapWidth);
canvas.setAttribute('height', staticMapHeight);
staticMapCanvas = canvas.getContext('2d');
// Initialize UI date/time
var currentDate = new Date();
var tomorrow = new Date(currentDate.getTime() + 86400000);
travelTimeDaySelect.selectedIndex = tomorrow.getDate() - 1;
travelTimeMonthSelect.selectedIndex = tomorrow.getMonth();
// UI callbacks
for (var i = 0; i < typeRadios.length; ++i)
typeRadios[i].onclick = refreshURL;
for (var i = 0; i < travelModeRadios.length; ++i)
travelModeRadios[i].onclick = refreshURL;
for (var i = 0; i < transitModeChecks.length; ++i)
transitModeChecks[i].onclick = refreshURL;
for (var i = 0; i < timeTypeRadios.length; ++i)
timeTypeRadios[i].onclick = refreshURL;
document.getElementById('gridSizeField').onchange = refreshURL;
// Special 'travelMode' UI callback
var transitModesDiv = document.getElementById('transitModesDiv')
var onTravelModeChanged = function() {
for (var i = 0; i < travelModeRadios.length; ++i) {
var radio = travelModeRadios[i];
if (radio.value === 'TRANSIT')
transitModesDiv.style.display = radio.checked ? 'block' : 'none';
}
refreshURL();
}
for (var i = 0; i < travelModeRadios.length; ++i)
travelModeRadios[i].onclick = onTravelModeChanged;
// Read params off URL hash
var params = window.location.hash.substring(1);
params = params.split('&');
params = params.map(p=>p.split('='));
params = params.filter(e=>Array.isArray(e) && e.length == 2);
for (var i = 0; i < params.length; ++i) {
var key = params[i][0];
var value = params[i][1];
if (key == 'type') {
document.forms['typeForm'][value].checked = true;
}
else if (key == 'travelMode') {
document.forms['travelModeForm'][value].checked = true;
}
else if (key == 'transitMode') {
// Clear all checks
for (var j = 0; j < transitModeChecks.length; j++)
transitModeChecks[j].checked = false;
// Set any check found in value
var modes = value.split(',');
for (var j = 0; j < modes.length; ++j)
document.forms['transitModeForm'][modes[j]].checked = true;
}
else if (key == 'gridSize') {
gridSizeField.value = Number(value);
document.getElementById('gridSizeField').value = value;
}
else if (key == 'targetLocation') {
var latlng = value.split(',');
targetLocation = { lat: Number(latlng[0]), lng: Number(latlng[1]) };
}
else if (key == 'travelTime') {
travelTimeHoursSelect.selectedIndex = Number(value);
advancedSearchCheck.checked = true;
}
else if (key == 'travelMonth') {
travelTimeMonthSelect.selectedIndex = Number(value);
}
else if (key == 'travelDay') {
advancedSearchCheck.checked = true;
travelTimeDaySelect.selectedIndex = Number(value);
}
else if (key == 'trafficModel') {
for (var j = 0; j < trafficModelRadios.length; j++)
trafficModelRadios[j].checked = false;
trafficModelRadios[value].checked = true;
}
else if (key == 'timeType') {
for (var j = 0; j < timeTypeRadios.length; j++)
timeTypeRadios[j].checked = false;
timeTypeRadios[value].checked = true;
}
}
refreshDivs();
// Create Google Maps API objects
bounds = new google.maps.LatLngBounds;
map = new google.maps.Map(document.getElementById('map'), { center: targetLocation, zoom: 11, clickableIcons: false });
service = new google.maps.DistanceMatrixService;
polyInfoWindow = new google.maps.InfoWindow({content: "hello" });
setTargetLocation(targetLocation);
map.setCenter(targetLocation);
// Create search bar
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
// commented out here to better set positioning controls via CSS
// map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0)
return;
// delete old markers
for (var i = 0; i < markersArray.length; i++)
markersArray[i].setMap(null);
markersArray = [];
// For each place, get the icon, name and location.
bounds = new google.maps.LatLngBounds;
places.forEach(function(place) {
if (!place.geometry)
return;