-
Notifications
You must be signed in to change notification settings - Fork 23
/
heatmap.html
83 lines (66 loc) · 2.58 KB
/
heatmap.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
---
layout: default
title: Realtime Usage
no_edit_link: true
---
<div class="status">
<div id="map"></div>
<script>
var maxHeatPoints = 5000;
var maxMindAttribution = 'This product includes GeoLite data created by <a href="https://www.maxmind.com">MaxMind</a>';
var layerOsmMapnik = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors | ' + maxMindAttribution,
});
var layerHeatAll = L.heatLayer([], {
minOpacity: .4,
radius: 20,
attribution: maxMindAttribution, // does not work
});
var map = L.map('map', {
layers: [layerOsmMapnik, layerHeatAll],
fullscreenControl: true,
maxBounds: [[90,-180], [-90,180]],
}).fitBounds([[75,-160], [-45,160]]);
var UsersOnlineControl = L.Control.extend({
initialize: function (options) {
L.Util.setOptions(this, options);
},
onAdd: function(map) {
this._div = L.DomUtil.create('div', 'infobox');
this.update('???');
return this._div;
},
update: function(active) {
this._div.innerHTML = 'Estimated number of c:geo users in online mode: ' + active;
},
});
var info = new UsersOnlineControl({
position: 'topright'
}).addTo(map);
var heatmapData = [];
function updateHeatmap(clients, active) {
clients.map(function(client) {
heatmapData.push([client.latitude, client.longitude]);
});
var limit = Math.min(maxHeatPoints, active);
if (heatmapData.length > limit) {
heatmapData.splice(0, heatmapData.length - limit);
}
layerHeatAll.setLatLngs(heatmapData);
}
var wsLastRequest = 0;
function startWebsocket() {
var ws = new WebSocket("wss://status.cgeo.org/api/locations?initial=" + maxHeatPoints + "×tamp=" + wsLastRequest);
ws.onmessage = function(msg) {
var data = JSON.parse(msg.data);
wsLastRequest = data.timestamp;
updateHeatmap(data.clients, data.located);
info.update(data.active);
};
ws.onclose = function(event) {
setTimeout(function(){ startWebsocket(); }, 5000);
}
}
startWebsocket();
</script>
</div>