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
|
// different basemap
// L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
// attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
// }).addTo(map);
const Races = {
"White" : "European or White",
"eAsian" : "East Asian",
"sAsian" : "South Asian",
"Black" : "Black or African American",
"Aboriginal": "Aboriginal",
"Other" : "Other",
};
function plotPointsOnMap(points) {
L.geoJson(points, {
pointToLayer: function (feature, latlng) {
//return L.circleMarker(latlng);
latlngbounds.extend(latlng);
return L.marker(latlng);
}
}).on('click', showDetails).addTo(map);
// rezoom the map so that all the markers fit in the view, add 20% padding so
// that marker dont cut off
map.fitBounds(latlngbounds.pad(0.20));
}
// show details about point
// e is the event info
function showDetails(e) {
// layer.feature.geometry gives you access to all the fields
let layer = e.layer;
let sideBar = document.getElementById('sidebar');
if (getComputedStyle(sideBar).visibility === 'hidden') {
sideBar.style.visibility = 'visible';
}
// remove the previous report text
let report = document.getElementById('report');
let prevUserInputs = report.getElementsByClassName('user-input');
for (let i=0; i < prevUserInputs.length; i++) {
prevUserInputs[i].innerHTML = "";
}
// put gender of the person
let genderTextSpan = document.getElementById('report-gender');
genderTextSpan.className = 'user-input';
let genderText = document.createTextNode(layer.feature.geometry['gender']);
genderTextSpan.appendChild(genderText);
// put age range of person
let ageRangeTextSpan = document.getElementById('report-age-range');
ageRangeTextSpan.className = 'user-input';
let ageRangeText = document.createTextNode(
" ~ " + layer.feature.geometry['age'] + " years");
ageRangeTextSpan.appendChild(ageRangeText);
// put race of person
let raceTextSpan = document.getElementById('report-race');
raceTextSpan.className = 'user-input';
let raceText = document.createTextNode(
Races[layer.feature.geometry['race']]
);
raceTextSpan.appendChild(raceText);
// put other attributes
let otherAttrTextSpan = document.getElementById('report-other');
otherAttrTextSpan.className = 'user-input';
otherAttrTextSpan.innerHTML =
"Long hair? " + (layer.feature.geometry['longhair'] ? "Yes" : "No") +
"<br>" +
"Long beard? " + (layer.feature.geometry['longbeard'] ? "Yes" : "No");
// put extra info
let extraTextSpan = document.getElementById('report-distinctive');
extraTextSpan.className = 'user-input';
let extraText = document.createTextNode(layer.feature.geometry['extra']);
extraTextSpan.appendChild(extraText);
let closeBtn = document.getElementById('close-btn')
closeBtn.addEventListener('click', closeDetails)
}
function closeDetails(e) {
let details = document.getElementById('sidebar')
details.style.visibility = 'hidden'
}
// different basemap
L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
subdomains: 'abcd',
minZoom: 0,
maxZoom: 18,
ext: 'png'
}).addTo(map);
// keep track of the boundary of the markers so that we can update the
// map to fit them all
var latlngbounds = new L.latLngBounds();
plotPointsOnMap(points);
if (window.EventSource) {
var source = new EventSource('/stream');
source.addEventListener('message', function(e) {
var data = JSON.parse(e.data);
if (data.coordinates) {
points.push(data);
plotPointsOnMap(points);
}
}, false)
source.addEventListener('open', function(e) {
console.log("Connection was opened")
}, false)
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
console.log("Connection was closed")
}
}, false)
} else {
console.log("sse not supported.");
}
|