-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
220 lines (174 loc) · 7.09 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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Trinity College CTtransit Bus Positions in Real Time</title>
<!-- Load jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Load Leaflet.js -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<!-- Load local styles -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="map"></div>
<script>
// Declare constants
var INIT_COORDS = [41.754046, -72.680830] // between main campus and downtown campus
var TRIN_COORDS = [41.7479, -72.6903];
var DOWNTOWN_COORDS = [41.766537, -72.670216];
// Initialize map
var map = L.map('map', {
center: INIT_COORDS,
zoom: 14,
maxZoom: 16, // Terrain basemap only allows up to 16
scrollWheelZoom: false,
});
// Add baselayer
L.tileLayer('https://stamen-tiles.a.ssl.fastly.net/terrain/{z}/{x}/{y}.jpg').addTo(map);
// Add bus routes (GeoJSON polylines)
[
{route: '37-39', color: '#008000'},
{route: '41', color: '#008000'},
{route: '61', color: '#ff0000'}
].map(function(o) {
$.getJSON('routes/' + o.route + '.geojson', function(geojson) {
var route = L.geoJSON(geojson, {style: {
color: o.color,
weight: 5,
opacity: 0.6
}
}).addTo(map);
});
});
// Add Trinity main campus icon
var trinity = L.marker(TRIN_COORDS, {
icon: L.icon({
iconUrl: 'icons/trinity.png',
iconSize: [64, 64],
popupAnchor: [0, -24],
})
}).bindPopup('Trinity College<br>300 Summit St', { autoClose: false })
.addTo(map)
.openPopup();
// Add Trinity downtown logo
var downtown = L.marker(DOWNTOWN_COORDS, {
icon: L.icon({
iconUrl: 'icons/trinity.png',
iconSize: [32, 32],
popupAnchor: [0, -16],
})
})
.bindPopup('Trinity College at<br>10 Constitution Plaza', { autoClose: false })
.addTo(map)
.openPopup();
// Add legend
var legend = L.control({ 'position': 'bottomright' });
legend.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'legend');
var content = '<h4>Bus routes near Trinity</h4>';
content += '<span class="line green-line"> </span> 37-39-41<br>';
content += '<span class="line red-line"> </span> 61 <br>';
content += '<img src="icons/bus-gray.png" class="icon"> Real-time bus locations<br>';
content += '<img src="icons/bus-stop.png" class="icon"> Recommended bus stops';
this._div.innerHTML = content;
return this._div;
};
legend.addTo(map);
// Add bus stops icons
[
{
name: 'New Britain Ave and Henry St (near The Tap)', // id: '2152',
lat: 41.743587, lon: -72.689772
},
{
name: 'Main St and Arch St (Hartford Public Library, City Hall, Wadsworth)', //id: '8028',
lat: 41.762445,lon: -72.674253
},
{
name: 'Main St and Pearl St (Gold Building, back to campus)', //id: '7841',
lat: 41.774076, lon: -72.521346
},
{
name: 'Broad St and Vernon St (Trinfo Cafe)', //id: '597',
lat: 41.751404, lon: -72.686959
},
{
name: 'Capitol Ave and State Library (State Capitol)', //id: '8025',
lat: 41.762884, lon: -72.683035
},
{
name: 'Pearl St and Lewis St (walk to 10 Constitution Plaza)', //id: '7897',
lat: 41.766092, lon: -72.675178
},
{
name: 'Main St and Gold St (across from Wadsworth Museum, back to campus)', //id: '53',
lat: 41.764276, lon: -72.673946
}
].map(function(o) {
L.marker([o.lat, o.lon], {icon: L.icon({
iconUrl: 'icons/bus-stop.png',
iconSize: [32, 32],
iconAnchor: [16, 32],
})
}).addTo(map).bindPopup(o.name);
});
// Load trips.json data file, fetch bus locations, and start updating
$.getJSON('gtfs/trips.json?1', function(trips) {
var markers = {};
var updatePositions = function() {
$.getJSON('https://cttransit.action-lab.org/', function(feed) {
for (var i in feed.entity) {
var obj = feed.entity[i];
var route = parseInt(obj.vehicle.trip.route_id);
var trip = parseInt(obj.vehicle.trip.trip_id);
var vehicleId = obj.vehicle.vehicle.id;
// Only display buses that follow relevant trips (from trips.json)
if ( trips[trip] ) {
var lat = obj.vehicle.position.latitude;
var lng = obj.vehicle.position.longitude;
// Only add same buses once
if ( !markers[vehicleId] ) {
var busNumber = trips[trip].trip_headsign.split(' ')[0];
var is61 = busNumber.indexOf('61') === 0; // `61` or `61D` etc
var bgColor = is61 ? 'red' : 'green';
var direction = trips[trip].direction_id + '';
var glyphContent = '\
<span style="\
background:' + bgColor + ';\
color: white;\
">' + busNumber + '</span>\
<br>\
<img src="icons/bus' + (is61 ? '-red' : '') + '.png" ' + (direction == '1' ? '' : ' class="downtown"') + '>';
var glyph = L.divIcon({
html: glyphContent,
iconSize: [40, 40],
iconAnchor: [20, 20],
className: 'bus-icon',
});
var popup = 'Bus ' + trips[trip].trip_headsign;
markers[vehicleId] = L.marker([lat, lng], {icon: glyph}).addTo(map).bindPopup(popup);
}
markers[vehicleId].setLatLng(new L.latLng(lat, lng));
}
}
}).fail(function() { console.log('Cannot load data')} );
setTimeout(updatePositions, 5000);
}
// This is the first and only call to updatePositions,
// then it starts calling itself via setTimeout
updatePositions();
});
// Edit credits and attribution
$('.leaflet-control-attribution')[0].innerHTML = 'View \
<a href="https://github.com/Action-Lab/cttransit-bus-map">code</a> on GitHub | '
+ $('.leaflet-control-attribution')[0].innerHTML;
</script>
</body>
</html>