-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
237 lines (217 loc) · 6.88 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
<!DOCTYPE html>
<html>
<head>
<title>Toronto Bike Share Web Map</title>
<link rel="shortcut icon" href="#">
<link rel="stylesheet" href="css/leaflet.css">
<script src="js/leaflet.js"></script>
<script src="js/leaflet.rotatedMarker.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
width: 100%;
}
.description {
width: 250px;
font-size: 16px;
color: #333333;
font-family: "Open Sans", Helvetica, sans-serif;
padding: 10px 14px;
background-color: rgba(245,245,220,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
border: 1px solid grey;
}
.dropdown {
min-width: 15em;
padding: 5px 10px 10px 10px;
background-color: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
.dropdown h3 {
margin: 5px 0px 0px 0px;
}
#route {
width: 100%;
padding: 6px 8px;
font: 14px/16px Arial, Helvetica, sans-serif;
background-color: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
.data_source {
padding: 6px 8px;
background-color: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
</style>
<!---disables unwanted scaling of the page when on mobile devices--->
<meta name="viewport" content="width=device-width,
initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<body>
<div id="map"></div>
<script>
let osm = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'});
let imagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'});
let dark = L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
})
let map = L.map('map', {center: [43.708563, -79.308769], zoom: 12, layers: [osm]});
// add a legend control for route
let legend = L.control({position: "topleft"});
legend.onAdd = function() {
let div = L.DomUtil.create("div", "dropdown");
div.innerHTML =
'<h3>Route</h3>' +
'<select id="route"></select><br>'
return div;
};
legend.addTo(map);
function parseXml(xml)
{
let gj = {
"name":"TTC Buses",
"type":"FeatureCollection",
"features":[]
};
$(xml).find("vehicle").each(function()
{
gj.features.push({"type": "Feature",
"geometry":{
"type": "Point",
"coordinates": [$(this).attr("lon")*1, $(this).attr("lat")*1]},
"properties":{
"ID": $(this).attr("id"),
"Route": $(this).attr("routeTag"),
"Heading": $(this).attr("heading"),
"Speed": $(this).attr("speedKmHr"),
"Updated": $(this).attr("secsSinceReport")
}
});
});
return gj
}
function get_ttc() {
ttc.clearLayers();
fetch("https://webservices.umoiq.com/service/publicXMLFeed?command=vehicleLocations&a=ttc&t=0")
.then(function(response) {
return response.text();
})
.then (function(xmlString) {
return $.parseXML(xmlString)
})
.then(function(xml) {
return parseXml(xml)
})
.then(function(data) {
L.geoJSON(data, {
filter: routeFilter,
pointToLayer: function(geojson_point, latlng) {
return L.marker(latlng, {
icon: arrow,
rotationAngle: geojson_point.properties.Heading
});
},
onEachFeature: function(feature, layer) {
layer.bindPopup(
'<div class="popup">' +
'Vehicle ID: ' + feature.properties.ID + "<br>" +
'Route: ' + feature.properties.Route + "<br>" +
'Heading: ' + feature.properties.Heading + "<br>" +
'Speed(km/h): ' + feature.properties.Speed + "<br>" +
'Last Updated(seconds): ' + feature.properties.Updated + "<br>" +
'</div>'
);
}
}).addTo(ttc)
});
}
// route filter function
function routeFilter(feature) {
if (document.getElementById("route").value === "All") {
return true
} else if (feature.properties.Route === document.getElementById("route").value) {
return true
}
}
// Update route dropdown menu
fetch("https://webservices.umoiq.com/service/publicXMLFeed?command=vehicleLocations&a=ttc&t=0")
.then(function(response) {
return response.text();
})
.then (function(xmlString) {
return $.parseXML(xmlString)
})
.then(function(xml) {
return parseXml(xml)
})
.then(
function(gj) {
let html = "<option>All</option>";
let array = []
gj.features.forEach(function(element) {
array.push(element.properties.Route)
});
array = [...new Set(array)]
array = array.sort()
array.forEach(function(element){
html += "<option>" + element + "</option>"
}
)
document.getElementById("route").innerHTML = html;
document.getElementById("route").addEventListener("change", get_ttc);
}
)
let ttc = L.layerGroup();
let arrow = L.icon({
iconUrl: './css/images/arrow.svg',
iconSize: [17, 17]
});
// make initial display
get_ttc()
ttc.addTo(map)
// update every 60 seconds
setInterval(get_ttc, 60000);
// add layer control
let baseMaps = {
"Dark OSM": dark,
"OSM": osm,
"Imagery": imagery
};
let overlay = {
"TTC Real Time Vehicle Locations": ttc
}
let layerControl = L.control.layers(baseMaps, overlay, {position: "topleft", collapsed: true}).addTo(map);
// add map title and description
let description = L.control({position: "topright"});
description.onAdd = function() {
let div = L.DomUtil.create("div", "description");
div.innerHTML =
'<p><b>Toronto Transit Commission Vehicle Real Time Locations</b></p><hr>' +
'<p>This web map displays real-time information of each Toronto Transit Commission (TTC) vehicle. The map updates every minute at most.</p>' +
'<a href="https://zehuiyin.github.io/" target="_blank">Zehui Yin</a> <br>' +
'Created with the <i>Leaflet</i> library, <i>OpenStreetMap</i>, and <i>UMO Public XML Feed</i><br>'
return div;
};
description.addTo(map);
// add data source
let data_source = L.control({position: "bottomleft"});
data_source.onAdd = function() {
let div = L.DomUtil.create("div", "data_source");
div.innerHTML =
'Data Source: Toronto Transit Commission, UMO'
return div;
};
data_source.addTo(map);
</script>
</body>
</html>