-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
184 lines (139 loc) · 4.35 KB
/
script.js
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
var map;
var userPosition;
var userPositionMarker;
var points = [];
const filesName = [
"Argentina_2019",
"Bolivia_Argentina_2018",
"Bolivia_Peru_2020",
"Brasil_2017",
"Brasil_2019",
"Brasil_2021",
"Colombia_2016",
"Ecuador_2014",
"Europa_2016",
"Mexico_2017"
];
const colors = [
"blue",
"purple",
"orange",
"seagreen",
"green",
"darkgreen",
"darkred",
"orangered",
"darkcyan",
"darkblue"
];
function createMap(){
// crete the map
map = L.map('map');
// download the map image
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
}
function getUserPosition() {
if ('geolocation' in navigator){
console.log('geolocation available');
navigator.geolocation.getCurrentPosition(position => {
userPosition = position.coords;
let lat = userPosition.latitude;
let lng = userPosition.longitude;
map.setView([ lat,lng ]);
if (userPositionMarker != undefined) userPositionMarker.remove();
userPositionMarker = L.marker([lat,lng]).addTo(map);
});
} else {
alert('geolocation not available');
}
}
async function getData(nomeArquivo) {
let xs = [], ys = [];
const url = `geolocationsCSV/${nomeArquivo}.csv`;
const response = await fetch(url);
const data = await response.text();
const rows = data.split('\n');
for (let row of rows){
const cols = row.split(',');
xs.push( parseFloat( cols[0] ));
ys.push( parseFloat( cols[1] ));
}
return { xs: xs , ys: ys};
}
async function renderPoints(nomeArquivo,color){
const data = await getData(nomeArquivo);
const xs = data.xs;
const ys = data.ys;
for (let i = 0 ; i < xs.length ; i ++){
// cria ponto sobre o mapa
var circle = L.circle([ ys[i] , xs[i] ], {
color: color,
radius: 10
}).addTo(map);
points.push(circle);
}
}
// criar checkboxes criarOpcoesHTML
function criarOpcoesHTML() {
const elemento = document.querySelector(".opcoes");
for (let i = 0 ; i < filesName.length ; i++) {
let file = filesName[i];
let color = colors[i];
elemento.innerHTML += `
<div class="opcao">
<input type="checkbox" id="${file}" name="${file}">
<label style="color: ${color};" for="${file}">${file.replace(/_/g," ")}</label>
</div>
`;
}
}
function plusBtn() {
const elemento = document.querySelector(".footer");
elemento.classList.toggle("open");
}
////
////
////
criarOpcoesHTML();
createMap();
map.setView([ 51.509865 , -0.118092 ], 13 );
async function renderSelected() {
plusBtn();
let filesSelected = 0;
for (let file of filesName){
let checkbox = document.getElementById(file);
if (checkbox.checked) filesSelected++;
}
if ( points.length == 0 && filesSelected == 0 ) return;
// remover pontos se houver
if ( points.length > 0 ) {
points.map(el => el.remove());
points = [];
}
if (filesSelected == 0) return;
// se ha pontos para plotar...
for (let i = 0 ; i < filesName.length ; i++){
let checkbox = document.getElementById(filesName[i]);
if (checkbox.checked) {
await renderPoints(filesName[i],colors[i]);
}
}
fitMapOnScreen();
}
function getLatPerPix() {
let northEastPoint = map.getBounds().getNorthEast().lat;
let southEastPoint = map.getBounds().getSouthEast().lat;
let mapHeightInLat = northEastPoint - southEastPoint;
let mapHeightInPixels = map.getSize().y;
return mapHeightInLat / mapHeightInPixels;
}
function fitMapOnScreen() {
let group = new L.featureGroup(points);
let latCorr = group.getBounds()._southWest.lat - 60*getLatPerPix();
let p1 = L.marker( group.getBounds()._northEast );
let p2 = L.marker([ latCorr , group.getBounds()._southWest.lng ]);
group = new L.featureGroup([p1,p2]);
map.fitBounds( group.getBounds() );
}