forked from cheeaun/busrouter-sg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
929 lines (846 loc) · 29.4 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
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
<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/WebPage">
<head>
<meta charset="utf-8">
<title>Singapore Bus Routes Explorer</title>
<meta name="description" content="Explore bus stops and routes on the map for all bus services in Singapore">
<meta name="apple-mobile-web-app-title" content="BusRouter SG">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="google" value="notranslate">
<meta itemprop="image" content="assets/icons/icon-144.png">
<meta itemprop="description" content="Explore bus stops and routes on the map for all bus services in Singapore">
<meta property="fb:app_id" content="165047640357519">
<meta property="og:type" content="website">
<meta property="og:url" content="http://cheeaun.github.io/busrouter-sg/">
<meta property="og:title" content="Singapore Bus Routes Explorer">
<meta property="og:description" content="Explore bus stops and routes on the map for all bus services in Singapore">
<meta property="og:image" content="http://cheeaun.github.io/busrouter-sg/assets/icons/icon-200.png">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<link rel="shortcut icon" href="favicon.ico">
<link rel="apple-touch-icon-precomposed" href="assets/icons/icon-144.png">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:300,400,500">
<link rel="stylesheet" href="assets/css/style.css">
<link rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/kmoebclbglclobmahimdaniikogclifn">
<script>
if (location.hostname == 'cheeaun.github.io'){
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23235796-4']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
var _rollbarParams = {
"server.environment": "production",
"client.javascript.source_map_enabled": true,
"client.javascript.guess_uncaught_frames": true
};
_rollbarParams["notifier.snippet_version"] = "2";
var _rollbar=["d34791fd62784f70920774893c1c6323", _rollbarParams];
var _ratchet=_rollbar;
(function(w,d){w.onerror=function(e,u,l){_rollbar.push({_t:'uncaught',e:e,u:u,l:l});};var i=function(){var s=d.createElement("script");var
f=d.getElementsByTagName("script")[0];s.src="//d37gvrvc0wt4s1.cloudfront.net/js/1/rollbar.min.js";s.async=!0;
f.parentNode.insertBefore(s,f);};if(w.addEventListener){w.addEventListener("load",i,!1);}else{w.attachEvent("onload",i);}})(window,document);
}
</script>
</head>
<body ontouchstart><!-- make a:active work on iOS Mobile Safari -->
<!--[if lt IE 9]>
<div id="outdated-browser">
<p>You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
</div>
<![endif]-->
<header>
<h1><a href="#/"><img src="assets/images/logo.png" width="16" height="20" alt=""> <span itemprop="name">Singapore Bus Routes Explorer</span></a></h1>
<form id="places-search-form">
<input type="search" placeholder="Search for a place">
</form>
<a id="header-about">About</a>
<a id="header-sidebar" class="active">☰</a>
</header>
<div id="about" class="hidden">
<section>
<a class="close">×</a>
<p><b>Singapore Bus Routes Explorer</b> (or <b>BusRouter SG</b>) lets you explore bus stops and routes on the map for all bus services in Singapore.</p>
<p>This is <a href="https://github.com/cheeaun/busrouter-sg" target="_blank">yet another project</a> by <a href="http://twitter.com/cheeaun" target="_blank" itemprop="creator">@cheeaun</a>, works best on <strong>modern web browsers</strong> and the data is <em>extracted</em> from the <a href="http://www.publictransport.sg/">Land Transport Authority</a>. Feel free to <a href="http://twitter.com/cheeaun" target="_blank">send feedback</a>, <a href="https://github.com/cheeaun/busrouter-sg/issues" target="_blank">report any issues</a> and share this with your friends and family.</p>
<div class="fb-like" data-href="http://cheeaun.github.io/busrouter-sg/" data-width="120" data-layout="button_count" data-show-faces="false" data-send="false"></div>
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://cheeaun.github.io/busrouter-sg/" data-text="Singapore Bus Routes Explorer" data-related="cheeaun">Tweet</a>
</section>
</div>
<aside id="sidebar">
<section id="bus-services-section">
<h1>Bus Services</h1>
<div id="bus-services"></div>
</section>
<section id="bus-routes-section" class="extra hidden">
<h1>Routes for Bus Service <b></b> <a href="#/" class="close">×</a></h1>
<div id="bus-routes"></div>
</section>
<section id="bus-stops-section" class="extra hidden">
<h1>Bus Stops <a href="#" class="close">×</a></h1>
<div id="bus-stops"></div>
</section>
<section id="bus-stop-routes-section" class="extra hidden">
<h1>Routes for Bus Stop <b></b> <a href="#/" class="close">×</a></h1>
<div id="bus-stop-routes"></div>
</section>
</aside>
<div id="map"></div>
<div id="bounds-warning">Looks like you're lost. <a href="#" id="back-sg">Go back to Singapore</a>.</div>
<script src="http://busrouter-sg.s3-ap-southeast-1.amazonaws.com/js/gzip-enabled.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDpk0BS7iLdbn5U545tiIN12k1OCgj2cc4&libraries=places&sensor=false"></script>
<script src="js/scripts.js"></script>
<script>
(function(){
if (!window.addEventListener) return; // If the browser doesn't even support this, just give up.
var dataVersion = '2'; // Versioning for all the bus data
var currentDataVersion = lscache.get('busDataVersion');
if (dataVersion != currentDataVersion && localStorage && localStorage.length && localStorage.removeItem){
// Remove amplify's localStorage
for (var i = localStorage.length-1; i >= 0 ; --i) {
var key = localStorage.key(i);
if (key.indexOf('__amplify__bus') === 0) {
localStorage.removeItem(key);
}
}
lscache.flush();
lscache.set('busDataVersion', dataVersion);
}
if (/mobi/i.test(navigator.userAgent)){
var scrollTop = function(){
var body = document.body;
setTimeout(function(){
body.style.height = screen.height + 'px';
window.scrollTo(0, 0);
body.style.height = window.innerHeight + 'px';
}, 300);
};
scrollTop();
window.onresize = scrollTop;
}
var sprite = {
url: 'assets/images/bus-sprite.png',
scaledSize: new google.maps.Size(156/2, 168/2)
};
var markerImage = {
location: {
url: sprite.url,
scaledSize: sprite.scaledSize,
size: new google.maps.Size(36/2, 38/2),
anchor: new google.maps.Point(36/4, 38/4),
origin: new google.maps.Point(96/2, 0)
},
circle: {
url: sprite.url,
scaledSize: sprite.scaledSize,
size: new google.maps.Size(36/2, 38/2),
anchor: new google.maps.Point(36/4, 38/4),
origin: new google.maps.Point(60/2, 0)
},
dot: {
url: sprite.url,
scaledSize: sprite.scaledSize,
size: new google.maps.Size(52/2, 76/2),
origin: new google.maps.Point(0, 92/2)
},
a: {
url: sprite.url,
scaledSize: sprite.scaledSize,
size: new google.maps.Size(52/2, 76/2),
origin: new google.maps.Point(52/2, 92/2)
},
b: {
url: sprite.url,
scaledSize: sprite.scaledSize,
size: new google.maps.Size(52/2, 76/2),
origin: new google.maps.Point(104/2, 92/2)
}
};
var rootEndPoints = {
github: 'data/2/',
s3: 'http://busrouter-sg.s3-ap-southeast-1.amazonaws.com/v2/'
};
var dataEndPoints = {
busStops: 'bus-stops.json',
busServices: 'bus-services.json',
busService: 'bus-services/{{no}}.json',
busStopsServices: 'bus-stops-services.json'
};
var getData = function(key, params, done){
if (arguments.length == 2){
done = params;
params = {};
}
var data = lscache.get(key);
if (data){
done(null, data);
} else {
var xhr = new XMLHttpRequest();
var useS3 = 'withCredentials' in xhr && typeof GZIP_ENABLED != 'undefined' && GZIP_ENABLED;
var endPoint = rootEndPoints[useS3 ? 's3' : 'github'] + dataEndPoints[key.split('-')[0]];
for (p in params) endPoint = endPoint.replace('{{' + p + '}}', params[p]);
xhr.onload = function(){
try{
var data = JSON.parse(this.responseText);
done(null, data);
lscache.set(key, data, 24*60);
} catch (e){
done(e);
}
};
xhr.onerror = xhr.onabort = done;
xhr.open('get', endPoint, true);
xhr.send();
}
};
var isSmallScreen = window.innerWidth <= 640;
var q = queue();
[
function(callback){
getData('busStops', callback);
},
function(callback){
getData('busServices', callback);
},
function(callback){
getData('busStopsServices', callback);
},
function(callback){
window.onload = function(){
google.maps.visualRefresh = true;
var center = new google.maps.LatLng(1.3520830, 103.8198360);
var map = new google.maps.Map(document.getElementById('map'), {
center: center,
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
overviewMapControl: true,
overviewMapControlOptions: {
opened: true
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.RIGHT_BOTTOM
}
});
if (isSmallScreen){
map.setOptions({
overviewMapControl: false,
zoomControl: false
});
}
var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(1.25352193438281, 103.62192147229632), new google.maps.LatLng(1.50129709375363, 103.99983438993593));
map.fitBounds(bounds);
if (navigator.geolocation){
var geolocationControl = document.createElement('a');
geolocationControl.id = 'geolocation-control';
geolocationControl.href = '#';
geolocationControl.innerHTML = '<i></i>';
map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(geolocationControl);
var locationMarker = new google.maps.Marker({
map: map,
clickable: false,
title: 'Current location',
icon: markerImage.location,
cursor: 'default',
visible: false,
zIndex: 9001
});
var locationCircle = new google.maps.Circle({
map: map,
clickable: false,
fillColor: '#00b4d2',
fillOpacity: 0.1,
strokeWeight: 0,
visible: false,
zIndex: 9000
});
var watching = false, watch;
var unwatch = function(){
navigator.geolocation.clearWatch(watch);
watching = false;
locationCircle.setVisible(false);
locationMarker.setVisible(false);
$('#geolocation-control').removeClass('active');
};
$('#map').on('click', '#geolocation-control', function(e){
e.preventDefault();
if (watching){
var pos = locationMarker.getPosition();
if (map.getBounds().contains(pos)){
unwatch();
} else {
map.panTo(pos);
}
} else {
var panToLocation = false;
watch = navigator.geolocation.watchPosition(function(position){
watching = true;
var coords = position.coords;
var pos = new google.maps.LatLng(coords.latitude, coords.longitude);
locationCircle.setCenter(pos);
locationCircle.setRadius(coords.accuracy);
locationCircle.setVisible(true);
locationMarker.setPosition(pos);
locationMarker.setVisible(true);
if (!panToLocation && bounds.contains(pos)){
map.panTo(pos);
panToLocation = true;
}
$('#geolocation-control').addClass('active');
}, function(e){
unwatch();
alert('Unable to get your location. Please try again.');
}, {
enableHighAccuracy: true,
timeout: 60*1000, // 1 min timeout
maximumAge: 5*1000 // 5-second cache
})
}
});
}
var $boundsWarning = $('#bounds-warning');
google.maps.event.addListener(map, 'bounds_changed', function(){
var mapBounds = map.getBounds();
if (mapBounds.intersects(bounds)){
$boundsWarning.removeClass('visible');
} else {
$boundsWarning.addClass('visible');
}
});
$('#back-sg').on('click', function(e){
e.preventDefault();
$boundsWarning.removeClass('visible');
map.panTo(center);
setTimeout(function(){
map.fitBounds(bounds);
}, 300);
});
if (!isSmallScreen){
var $form = $('#places-search-form');
$form.on('submit', function(e){
e.preventDefault();
});
var autocomplete = new google.maps.places.Autocomplete($form.find('input')[0], {
bounds: bounds,
componentRestrictions: {country: 'sg'}
});
google.maps.event.addListener(autocomplete, 'place_changed', function(){
var place = autocomplete.getPlace();
if (!place.geometry) return;
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
});
}
callback(null, map);
};
}
].forEach(function(task){
q.defer(task);
});
q.awaitAll(function(error, results){
if (error || !results || results.length != 4){
alert('Oops, an error occured.');
return;
}
var busStops = results[0];
var busServices = results[1];
var busStopsServices = results[2];
var map = results[3];
var busStopsMap = {};
busStops.forEach(function(stop){
busStopsMap[stop.no] = stop;
});
var busServicesByType = {};
var busServicesMap = {};
busServices.services.forEach(function(service){
var type = service.type;
if (busServicesByType[type]){
busServicesByType[type].push(service);
} else {
busServicesByType[type] = [service];
}
busServicesMap[service.no] = service;
});
var $busServices = $('#bus-services');
var html = '';
for (type in busServicesByType){
var services = busServicesByType[type];
html += '<h2>' + busServices.types[type] + '</h2><ul>';
for (var i=0, l=services.length; i<l; i++){
var service = services[i];
var no = service.no;
html += '<li><a href="#/services/' + no + '" id="service-' + no + '">' + no + '</a></li>';
}
html += '</ul>';
}
$busServices.html(html);
var markers = [];
var polylines = {};
var infowindow = new google.maps.InfoWindow({
maxWidth: 260
});
var polyline = new google.maps.Polyline({
clickable: false,
strokeColor: '#f01b48',
strokeWeight: 5,
strokeOpacity: .5
});
var clearMap = function(){
markers.forEach(function(marker){
marker.setMap(null);
google.maps.event.clearInstanceListeners(marker);
});
markers = [];
for (var stop in stopMarkers){
var marker = stopMarkers[stop];
marker.setMap(null);
google.maps.event.clearInstanceListeners(marker);
}
stopMarkers = {};
for (var service in polylines){
var line = polylines[service];
line.setMap(null);
google.maps.event.clearInstanceListeners(line);
}
polylines = {};
polyline.setMap(null);
infowindow.close();
};
google.maps.event.addListener(map, 'markerClick', function(data){
var marker = data.marker;
var stop = data.stop;
var currentService = data.currentService;
var hideShowRoutesLink = data.hideShowRoutesLink;
var info = busStopsMap[stop];
var name = info.name;
var services = busStopsServices[stop];
var html = '<div class="infowindow"><h1>' + name + '</h1>'
+ '<div class="infostop">Bus stop number: <span class="tag">' + stop + '</span></div>';
services.forEach(function(service){
if (service == currentService){
html += '<span class="infoservice">' + service + '</span> ';
} else {
html += '<a href="#/services/' + service + '" class="infoservice">' + service + '</a> ';
}
});
if (services.length > 1 && !hideShowRoutesLink){
html += '<hr><a href="#/stops/' + stop + '" class="show-routes">Show all routes passing here</a>';
}
html += '</div>';
infowindow.setContent(html);
infowindow.open(map, marker);
});
// Get bounds of ALL bus stops
// var allBounds = new google.maps.LatLngBounds();
// busStops.forEach(function(stop){
// var position = new google.maps.LatLng(stop.lat, stop.lng);
// allBounds.extend(position);
// });
// console.log(allBounds.toString())
var stopMarkers = {};
google.maps.event.addListener(map, 'idle', function(){
if (currentRoute != 'home') return;
var $busStops = $('#bus-stops');
var zoom = map.getZoom();
if (zoom >= 15){
$('#bus-stops-section').removeClass('hidden');
var bounds = map.getBounds();
for (var no in stopMarkers){
var m = stopMarkers[no];
if (!bounds.contains(m.getPosition())){
m.setMap(null);
stopMarkers[no] = undefined;
try { // Looks like iOS 6 choke on this
delete stopMarkers[no];
} catch (e) {}
}
}
var busStopsOnMap = [];
busStops.forEach(function(stop){
var no = stop.no;
if (!stopMarkers[no]){
var position = new google.maps.LatLng(stop.lat, stop.lng);
if (!bounds.contains(position)) return;
var marker = new google.maps.Marker({
position: position,
map: map,
title: stop.name,
icon: markerImage.circle
});
google.maps.event.addListener(marker, 'click', function(){
google.maps.event.trigger(map, 'markerClick', {
marker: marker,
stop: no
});
});
stopMarkers[no] = marker;
}
busStopsOnMap.push({
no: no,
stop: stop
});
});
if (busStopsOnMap.length){
var html = '<ul>';
busStopsOnMap.sort(function(a, b){
var aName = a.stop.name;
var bName = b.stop.name;
if (aName < bName) return -1;
if (aName > bName) return 1;
return 0;
});
busStopsOnMap.forEach(function(b){
var no = b.no;
var name = b.stop.name;
html += '<li><a class="stop" id="stop-' + no + '"><span class="tag">' + no + '</span> ' + name + '</a></li>';
});
html += '</ul>';
$busStops.html(html);
} else {
$busStops.html('<p>Try zoom in or pan around to see bus stops on the map.</p>');
}
} else {
for (var no in stopMarkers) stopMarkers[no].setMap(null);
stopMarkers = [];
$busStops.html('<p>Try zoom in or pan around to see bus stops on the map.</p>');
}
});
$('#bus-stops').on('click', 'li a', function(e){
e.preventDefault();
var stop = $(this).find('.tag').text();
var marker = stopMarkers[stop];
var latlng = marker.getPosition();
map.panTo(latlng);
google.maps.event.trigger(marker, 'click');
});
$('#bus-stops-section').on('click', 'a.close', function(e){
e.preventDefault();
$('#bus-stops-section').addClass('hidden');
});
var $busRoutes = $('#bus-routes');
var currentNo;
var docTitle = document.title;
var currentRoute = 'home';
ruto
.config({
notfound: function(){
ruto.go('/');
}
})
.add('/', function(){
currentRoute = 'home';
document.title = docTitle;
$('section.extra').addClass('hidden');
clearMap();
setTimeout(function(){
$busServices.find('a.selected').removeClass('selected');
}, 400);
})
.add(/^\/services\/(\w+)\/?(\d)?$/i, function(path, no, route){
currentRoute = 'services';
document.title = 'Bus service ' + no + ' - ' + docTitle;
if (!route) route = 1;
// reset
$busServices.find('a.selected').removeClass('selected');
$('#service-' + no).addClass('selected');
clearMap();
$busServicesSection = $('#bus-services-section').addClass('loading');
getData('busService-' + no, {no: no}, function(e, data){
$busServicesSection.removeClass('loading');
if (!data) return;
var one = data[route];
var routes = one.route;
var stops = one.stops;
var latlngs = [];
var locations = [];
for (var i=0, l=routes.length; i<l; i++){
var coord = routes[i];
var latlng = coord.split(',');
var position = new google.maps.LatLng(parseFloat(latlng[0], 10), parseFloat(latlng[1], 10));
latlngs.push(position);
}
polyline.setPath(latlngs);
polyline.setMap(map);
var html = '';
var provider = busServicesMap[no].operator;
if (provider){
// Only SBS or SMRT, for now.
var url = provider == 'sbs' ? 'http://www.sbstransit.com.sg/journeyplan/servicedetails.aspx?serviceno=' : 'http://www.smrtbuses.com.sg/ebusguide/busfrequency.asp?BusNo=';
html += '<a href="' + url + no + '" target="_blank" class="details">See Bus Details and Schedules</a>';
}
if (data[2] && data[2].route && data[2].route.length){
html += '<a href="#/services/' + no + '" class="tab ' + (route == 1 ? 'selected' : '') + '">Route 1</a>'
+ '<a href="#/services/' + no + '/2" class="tab ' + (route == 2 ? 'selected' : '') + '">Route 2</a>';
}
html += '<ul>';
var firstSameLast = false;
var l = stops.length;
var bounds = new google.maps.LatLngBounds();
stops.forEach(function(stop, i){
var info = busStopsMap[stop];
var name = info.name;
var services = busStopsServices[stop];
var icon = markerImage.circle;
var last = l-1;
var className = 'stop';
if (i == 0){
var lastStop = stops[last];
var lastInfo = busStopsMap[lastStop];
if (name == lastInfo.name){
firstSameLast = true;
icon = markerImage.dot;
className = 'stop-dot';
} else {
icon = markerImage.a;
className = 'stop-a';
}
} else if (i == last){
if (firstSameLast){
icon = markerImage.dot;
className = 'stop-dot';
} else {
icon = markerImage.b;
className = 'stop-b';
}
}
if (i == last && firstSameLast){
// Push the first marker as the last one. MINDBLOWN!
markers.push(markers[0]);
} else {
var position = new google.maps.LatLng(info.lat, info.lng);
var marker = new google.maps.Marker({
position: position,
map: map,
icon: icon,
title: name,
animation: (i == 0 || i == last) ? google.maps.Animation.DROP : null,
zIndex: (i == 0 || i == last) ? 5 : 1
});
google.maps.event.addListener(marker, 'click', function(){
google.maps.event.trigger(map, 'markerClick', {
marker: marker,
stop: stop,
currentService: no
});
$busRoutes.find('li a.selected').removeClass('selected');
$('.stop-' + stop + '[data-index="' + i + '"]').addClass('selected').focus();
});
markers.push(marker);
bounds.extend(position);
}
html += '<li><a tabindex="0" href="#" class="' + className + ' stop-' + stop + '" data-index="' + i + '"><i></i><span class="tag">' + stop + '</span> ' + name + '</a></li>';
});
$busRoutes.html(html + '</ul>');
$busRoutes[0].scrollTop = 0;
$('#bus-routes-section h1 b').text(no);
$('section.extra').addClass('hidden');
$('#bus-routes-section').removeClass('hidden');
if (no != currentNo || !bounds.contains(map.getCenter())){
currentNo = no;
map.panTo(bounds.getCenter());
setTimeout(function(){
map.fitBounds(bounds);
}, 400);
}
lscache.set('busService-' + no, data, 24*60);
});
})
.add(/^\/stops\/(\w+)$/i, function(path, stop){
currentRoute = 'stops';
document.title = 'Bus stop ' + stop + ' - ' + docTitle;
clearMap();
$('#bus-stop-routes-section').removeClass('hidden').addClass('loading').find('h1 b').text(stop);
var stopData = busStopsMap[stop];
var position = new google.maps.LatLng(stopData.lat, stopData.lng);
var marker = new google.maps.Marker({
position: position,
map: map,
icon: markerImage.dot,
title: stopData.name,
animation: google.maps.Animation.DROP
});
google.maps.event.addListener(marker, 'click', function(){
google.maps.event.trigger(map, 'markerClick', {
marker: marker,
stop: stop,
hideShowRoutesLink: true
});
});
markers.push(marker);
var services = busStopsServices[stop];
var servicesBounds = new google.maps.LatLngBounds();
var html = '';
var run = function(data, service){
var d = data['1'];
var stops = d.stops;
// The stop could be in the second route instead and
// could also be the last stop of the route, so choose the second route
if (stops.indexOf(stop) == -1 || stops[stops.length-1] == stop){
d = data['2'];
stops = d.stops;
if (stops.indexOf(stop) == -1){
// Failsafe, if second route somehow doesn't have the stop for some reason
d = data['1'];
stops = d.stops;
}
}
stops.forEach(function(stopNo){
var stop = busStopsMap[stopNo];
servicesBounds.extend(new google.maps.LatLng(stop.lat, stop.lng));
});
var routes = d.route;
var latlngs = [];
var locations = [];
for (var i=0, l=routes.length; i<l; i++){
var coord = routes[i];
var latlng = coord.split(',');
var position = new google.maps.LatLng(parseFloat(latlng[0], 10), parseFloat(latlng[1], 10));
latlngs.push(position);
}
var line = new google.maps.Polyline({
strokeColor: '#f01b48',
strokeWeight: 5,
strokeOpacity: .4,
path: latlngs,
map: map
});
google.maps.event.addListener(line, 'mouseover', function(){
$('#stop-service-' + service).trigger('mouseover').focus();
});
google.maps.event.addListener(line, 'mouseout', function(){
$('#stop-service-' + service).trigger('mouseout').blur();
});
google.maps.event.addListener(line, 'click', function(){
location.href = $('#stop-service-' + service).attr('href');
});
polylines[service] = line;
html += '<li><a href="#/services/' + service + '" id="stop-service-' + service + '"><span class="tag">' + service + '</span> '+ busServicesMap[service].name
+ '</a></li>';
};
var q = queue();
services.forEach(function(no){
q.defer(function(callback){
getData('busService-' + no, {no: no}, function(e, serviceData){
run(serviceData, no);
callback();
});
});
});
q.awaitAll(function(){
map.fitBounds(servicesBounds);
$('#bus-stop-routes-section').removeClass('loading');
$('#bus-stop-routes').html('<ul>' + html + '</ul>');
});
})
.init();
$('#bus-routes').on('click', 'li a', function(e){
e.preventDefault();
var stopIndex = $(this).data('index');
var marker = markers[stopIndex];
if (!marker) return;
var latlng = marker.getPosition();
if (map.getZoom() < 15) map.setZoom(15);
map.panTo(latlng);
google.maps.event.trigger(marker, 'click');
if (isSmallScreen) $('#header-sidebar').trigger('click');
});
$('#bus-stop-routes').on('mouseover', 'li a', function(){
var serv = $(this).find('.tag').text();
for (var service in polylines){
var line = polylines[service];
if (service == serv){
line.setOptions({
strokeOpacity: .8
});
} else {
line.setOptions({
strokeOpacity: .1
});
}
}
}).on('mouseout', 'li a', function(){
for (var service in polylines){
polylines[service].setOptions({
strokeOpacity: .4
});
}
});
$(document).on('keyup', function(e){
if (!(/services\/.+/i).test(location.hash)) return;
if (e.target.tagName && e.target.tagName.toLowerCase() == 'input') return;
var key = e.keyCode;
switch (key){
case 39:
case 40:
var $selected = $busRoutes.find('li a.selected');
if (!$selected.length) return;
$selected.removeClass('selected');
var $nextLi = $selected.parent().next();
if (!$nextLi.length) return;
$nextLi.find('a').trigger('click');
break;
case 37:
case 38:
var $selected = $busRoutes.find('li a.selected');
if (!$selected.length) return;
$selected.removeClass('selected');
var $prevLi = $selected.parent().prev();
if (!$prevLi.length) return;
$prevLi.find('a').trigger('click');
break;
}
});
});
$('#header-about').on('click', function(){
$('#about').removeClass('hidden');
});
$('#about .close').on('click', function(){
$('#about').addClass('hidden');
});
if (isSmallScreen){
$('header h1 a').on('click', function(e){
e.preventDefault();
$('#about').toggleClass('hidden');
});
}
$('#bus-routes-section').on('click', 'a.details', function(e){
e.preventDefault();
var width = 640;
var height = 480;
var top = ((screen.availHeight || screen.height)-height)/2;
var left = (screen.width-width)/2;
window.open(this.href, 'busDetails', 'width=' + width + ',height=' + height + ',menubar=0,resizable=1,scrollbars=1,toolbar=0,top=' + top + ',left=' + left);
});
$('#header-sidebar').on('click', function(){
$('#sidebar').toggleClass('hidden');
$('#header-sidebar').toggleClass('active')
});
})();
</script>
<div id="fb-root"></div>
<script async>
setTimeout(function(){
// Facebook
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=165047640357519";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Twitter
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
}, 2000);
</script>
</body>
</html>