-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
map.html
277 lines (258 loc) · 11.9 KB
/
map.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
<!DOCTYPE html>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-S95TT7F01F"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-S95TT7F01F');
</script>
<!-- End of Global site tag (gtag.js) - Google Analytics -->
<title>OpenTrafficCamMap</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<script src="https://leafletjs.com/examples/choropleth/us-states.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest/dist/hls.min.js"></script>
<script src="https://unpkg.com/leaflet.markercluster@1.4.1/dist/leaflet.markercluster.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.Default.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet-easybutton@2/src/easy-button.css">
<script src="https://cdn.jsdelivr.net/npm/leaflet-easybutton@2/src/easy-button.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
width: 100%;
}
.leaflet-tooltip-pane .text {
color: black;
font-weight: bold;
background: transparent;
border:0;
box-shadow: none;
font-size:2em;
}
</style>
<script>
if(location.protocol !== 'http:') {
alert("It seems you are attempting to force SSL/TLS. HTTPS is not used for this site because it can cause issues with some of the traffic camera \
providers. Until they all support HTTPS we won't be able to. You can continue, but some streams may not work properly.");
}
</script>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map', {
worldCopyJump: true
}).setView([39, -100], 4);
L.easyButton({
states: [{
stateName: 'cluster-map',
icon: '<i class="material-icons" style="font-size:18px; padding-bottom: 3px; display: inline-flex; vertical-align: middle;">border_all</i>',
title: 'Change to Classic Heat Map Mode',
onClick: function(control) {
map.eachLayer(function (layer) {
map.removeLayer(layer);
});
DrawHeatMap();
control.state('heat-map');
}
}, {
stateName: 'heat-map',
icon: '<i class="material-icons" style="font-size:18px; padding-bottom: 3px; display: inline-flex; vertical-align: middle;">bubble_chart</i>',
title: 'Change to Cluster Map Mode',
onClick: function(control) {
map.eachLayer(function (layer) {
map.removeLayer(layer);
});
DrawClusterMap();
control.state('cluster-map');
}
}]
}).addTo(map);
var cameras;
fetch('./cameras/USA.json').then((response) => {return response.json();}).then((data) => {
cameras = data;
DrawClusterMap();
});
var hls;
function GetMarker(cam, county){
let m = L.marker([cam.latitude, cam.longitude]);
var cam_view = (() => {
if(cam.url){
switch( cam.format ) {
case "IMAGE_STREAM":
return `<img src="${cam.url}" width="320" height="240" alt="Camera Stream">`;
case "M3U8":
case "M3U9":
return '<video id="video" width="320" height="240" autoplay muted></video>';
case "UNIQUE_TEXASDOT":
return `The Texas DOT has a bizarre system making implementation of the camera on this site very difficult.
So, this should redirect you to a map of Texas by the state DOT that does work: <a href="https://its.txdot.gov/ITS_WEB/FrontEnd/default.html?r=TYL&p=Tyler&t=map">Link</a>`;
case "UNIQUE_COLORADODOT":
return `The Colorado DOT for an unknowable reason implements <a href="https://en.wikipedia.org/wiki/Digital_rights_management">DRM</a> making streaming on this site very difficult.
So, this should redirect you to a map of Colorado by the state DOT that does work: <a href="https://www.cotrip.org/map.htm#/default">Link</a>`;
default:
return "An error has occurred";
}
} else {
return "No Image";
}
})();
m = m.bindPopup(cam.description + '\n' + cam_view);
if(cam.format === "M3U8" || cam.format === "M3U9"){
m._url = cam.url
}
return m;
}
function DrawClusterMap(){
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var markers = L.markerClusterGroup({
maxClusterRadius: 40,
chunkedLoading: true
});
for(var state in cameras){
let count = 0;
for(var county in cameras[state]){
for(var camera in cameras[state][county]){
var cam = cameras[state][county][camera];
if(cam.latitude !== undefined && cam.longitude !== undefined){
markers.addLayer(GetMarker(cam, county));
}
}
}
}
map.addLayer(markers);
}
function DrawHeatMap(){
function DrawCams(state){
var layer = [];
for(var county in cameras[state]){
for(var i in cameras[state][county]){
let cam = cameras[state][county][i];
if(cam.latitude && cam.longitude){
let m = GetMarker(cam, county)
.openPopup();
layer.push(m);
}
}
}
return layer;
}
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var counts = new Map();
var highest_count = 0;
for(var state in cameras){
let count = 0;
for(var county in cameras[state]){
for(var camera in cameras[state][county]){
count++;
}
}
highest_count = (count > highest_count) ? count : highest_count;
counts.set(state, count);
}
function getColor(state){
var color = 0
if(counts.has(state)){
color = Math.floor((counts.get(state)/highest_count)*155) + 100;
}
return `#${color.toString(16)}0000`;
}
function style(feature){
return {
fillColor: getColor(feature.properties.name),
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7
};
}
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront();
}
}
function resetHighlight(e) {
geojson.resetStyle(e.target);
}
var camera_layer;
var tooltips = new Map();
function onClick(e) {
(camera_layer) ? map.removeLayer(camera_layer) : null;
var tooltip = tooltips.get(e.target.feature.properties.name);
if(tooltip){
map.removeLayer(tooltip);
} else {
tooltip = L.tooltip({
permanent: true,
direction: 'center',
className: 'text',
interactive: true
});
tooltip.setContent((counts.get(e.target.feature.properties.name)) ? counts.get(e.target.feature.properties.name) + '' : '');
tooltip.setLatLng(e.target.layer.getBounds().getCenter());
tooltip.addTo(map);
tooltips.set(e.target.feature.properties.name, tooltip);
}
tooltip.toggleTooltip();
camera_layer = L.layerGroup(DrawCams(e.target.feature.properties.name));
map.addLayer(camera_layer);
}
function listener(feature, layer) {
var tooltip = L.tooltip({
permanent: true,
direction: 'center',
className: 'text',
interactive: true
});
tooltip.setContent((counts.get(feature.properties.name)) ? counts.get(feature.properties.name) + '' : '');
tooltip.setLatLng(layer.getBounds().getCenter());
tooltip.addTo(map);
tooltips.set(feature.properties.name, tooltip);
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: onClick
});
}
var geojson = L.geoJson(statesData, {style: style, onEachFeature: listener}).addTo(map);
}
map.on('popupopen', (e) => {
if(e.popup._source._url){
hls = new Hls();
let video = document.getElementById('video');
hls.loadSource(e.popup._source._url);
hls.attachMedia(video);
}
});
map.on('popupclose', (e) => {
if(e.popup._source._url){
let video = document.getElementById('video');
hls.detachMedia();
hls.destroy();
video.remove();
}
});
</script>
</body>
</html>