-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
344 lines (298 loc) · 10.1 KB
/
script.js
File metadata and controls
344 lines (298 loc) · 10.1 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
const button1 = document.querySelector("#button1");
const mainText = document.querySelector("#main-text");
const addressText = document.querySelector("#address-text");
const slider1 = document.querySelector("#slider1");
const arrowContainer = document.querySelector("#arrow-container");
const mainArrow = document.querySelector("#main-arrow");
const mapContainer = document.querySelector("#map-container");
const mapView = document.querySelector("#view-div");
const gothereButton = document.querySelector('#gothere-button');
const dashboardContainer = document.querySelector("#dashboard-container");
const updateInterval = 500;
const updatePositionInterval = 1000;
let localLatitude;
let localLongitude;
let targetLatitude;
let targetLongitude;
let currentAngle = 0;
let targetAngle = 0;
let dashAngle = 0;
let alpha;
let beta;
let gamma;
let position;
let distance = 0;
let distanceUnit = "km";
let highAccuracy = false;
let speed = 0;
let gyroReady = false;
let positionReady = false;
let targetReady = false;
let mapReady = false;
let apiReady = false;
let currentActiveObject = mapView;
// TOGLIERE STA MERDA
//document.querySelector("#input1").value = localStorage.getItem("api-key") ?? "";
// Attivo sensore orientamento dispositivo
const sensor = new AbsoluteOrientationSensor();
Promise.all([navigator.permissions.query({ name: "accelerometer" }),
navigator.permissions.query({ name: "magnetometer" }),
navigator.permissions.query({ name: "gyroscope" })])
.then(results => {
if (results.every(result => result.state === "granted")) {
sensor.start();
console.log("Permessi presenti");
} else {
console.log("No permissions to use AbsoluteOrientationSensor.");
}
});
dashboardContainer.addEventListener('click', function(e)
{
dashAngle = (dashAngle - 90) % 360;
document.documentElement.style.setProperty("--dash-angle", dashAngle + "deg");
});
// Routine di aggiornamento orientamento
window.addEventListener("deviceorientationabsolute", function(e)
{
alpha = 360 - e.alpha;
beta = e.beta;
gamma = e.gamma;
gyroReady = true;
}, true);
// Recupero chiave mappa
const url = `/secrets/api.txt`;
let APIKey;
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
return response.text();
})
.then((text) => {
APIKey = text;
apiReady = true;
checkDisplayMap();
})
.catch((error) => console.log(error));
// Chiedo posizione per inizializzare mappa
navigator.geolocation.getCurrentPosition(
(pos) => {
position = pos;
mapReady = true;
checkDisplayMap();
}, // Posizione trovata
(error) => {console.log(error)}, // Posizione non trovata
{enableHighAccuracy: false}
);
function checkDisplayMap()
{
if(mapReady && apiReady)
displayMap()
}
function displayMap()
{
localLongitude = position.coords.longitude;
localLatitude = position.coords.latitude;
require(["esri/config","esri/Map", "esri/views/MapView", "esri/widgets/Search", 'esri/geometry/Extent'], function (esriConfig,Map, MapView, Search) {
//esriConfig.apiKey = "";
esriConfig.apiKey = APIKey;
const map = new Map({
basemap: "arcgis-navigation" // Basemap layer service
});
const view = new MapView({
map: map,
center: [localLongitude, localLatitude], // Longitude, latitude
zoom: 14, // Zoom level
container: "view-div", // Div element
constraints: getConstraints(localLatitude, localLongitude),
rotationEnabled: false // Disables map rotation
});
const search = new Search({ //Add Search widget
view: view,
maxSuggestions: 3
});
search.on("search-complete", function(e) {
let latitude = e.results[0].results[0].feature.geometry.latitude;
let longitude = e.results[0].results[0].feature.geometry.longitude;
addressText.innerHTML = e.searchTerm;
this.view.constraints = getConstraints(latitude, longitude);
targetReady = true;
});
view.ui.add(search, "top-right"); //Add to the map
});
document.querySelector("#map-container").classList.add('opaque');
}
function getConstraints(latitude, longitude)
{
targetLatitude = latitude;
targetLongitude = longitude;
let latlongAllowance = 0.01;
return {
geometry:
{
type: "extent",
xmin: longitude - latlongAllowance,
ymin: latitude - latlongAllowance,
xmax: longitude + latlongAllowance,
ymax: latitude + latlongAllowance,
},
minScale: 30000, // User cannot zoom out beyond a scale of 1:500,000
maxScale: 0, // User can overzoom tiles
}
}
// Aggiorno posizione attuale
setInterval(function()
{
navigator.geolocation.getCurrentPosition(
function(position) // Posizione trovata
{
localLongitude = position.coords.longitude;
localLatitude = position.coords.latitude;
Speed.get(localLongitude, localLatitude, updatePositionInterval);
positionReady = true;
},
function(error) // Posizione non trovata
{
console.log(error);
positionReady = false;
},
{enableHighAccuracy: highAccuracy}
);
}, updatePositionInterval);
// Update grafica
setInterval(function()
{
if(!positionReady)
return;
if(!targetReady)
return;
if(!gyroReady)
return;
currentAngle = targetAngle;
if(targetLatitude != localLatitude)
targetAngle = 90 - Math.atan2((targetLatitude - localLatitude), (targetLongitude - localLongitude)) * (180 / Math.PI) - alpha;
else
targetAngle = 0;
let delta = ((((targetAngle - currentAngle) % 360) + 540) % 360) - 180;
targetAngle = currentAngle + delta;
document.documentElement.style.setProperty("--angle", targetAngle + "deg");
Distance.get();
document.querySelector("#distance").innerHTML = distance + distanceUnit;
document.querySelector("#speed").innerHTML = speed + "km/h";
}, updateInterval);
function transitionToArrow()
{
arrowContainer.classList.toggle('transition');
}
gothereButton.addEventListener("click", function(e)
{
if(targetReady)
{
mapContainer.classList.toggle('opaque');
arrowContainer.classList.toggle('opaque');
dashboardContainer.classList.toggle('opaque');
mapContainer.classList.toggle('transparent');
arrowContainer.classList.toggle('transparent');
dashboardContainer.classList.toggle('transparent');
}
});
class Speed {
static latlongCoeff = 111.32 * 3600; // km in 1 grado latlong * secondi in un'ora * millisecondi in un secondo
static lastX = null;
static lastY = null;
static lastSpeed = 0;
static cooldownFilter = 3;
static totalDeltaT;
constructor() {}
static get(newX, newY, dt) {
if(this.lastX != null)
{
if((speed == 0) || ((this.lastX != newX) || (this.lastY != newY)))
{
this.cyclesNumber = 1;
//dt /= 1000.0;
//dt *= this.cyclesNumber;
let diffXSquared = (this.lastX - newX)**2;
let diffYSquared = (this.lastY - newY)**2;
let tempSquaredDistance = diffXSquared + diffYSquared;
let tempDistance = Math.sqrt(tempSquaredDistance);
let newSpeed = Math.floor(this.latlongCoeff * tempDistance / this.totalDeltaT);
this.lastX = newX;
this.lastY = newY;
speed = newSpeed;
}
else
{
this.totalDeltaT += dt / 1000.0;
}
/*if(newSpeed > 0) {
if(this.countdownFilter == 0) {
speed = newSpeed;
debugger;
} else if(this.countdownFilter > 0) {
this.countdownFilter--;
debugger;
}
} else {
speed = 0;
this.countdownFilter = 3;
}*/
/*let acceleration = (newSpeed - this.lastSpeed) / dt;
if(acceleration > 10 || ((speed == 0) && (0 < newSpeed < 10)))
{
this.lastSpeed = Math.floor((4 * this.lastSpeed + newSpeed) / 5);
}
else
{
this.lastSpeed = newSpeed;
speed = newSpeed;
}*/
}
else
{
this.lastX = newX;
this.lastY = newY;
}
}
static setPosition(X, Y) {
this.lastX = X;
this.lastY = Y;
}
}
class Distance {
constructor() {}
static get() {
//SOURCE: https://www.movable-type.co.uk/scripts/latlong.html
const piCoeff = Math.PI/180;
const R = 6371e3; // metres
const φ1 = localLatitude * piCoeff; // φ, λ in radians
const φ2 = targetLatitude * piCoeff;
const Δφ = (targetLatitude-localLatitude) * piCoeff;
const Δλ = (targetLongitude-localLongitude) * piCoeff;
const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
let d = R * c; // in metres
if(d >= 1000)
{
highAccuracy = false;
distanceUnit = "km";
d /= 1000.0;
if(d >= 100)
d = Math.floor(d);
else if(d >= 10)
d = d.toFixed(1);
else
d = d.toFixed(2);
}
else
{
highAccuracy = true;
distanceUnit = "m";
d = Math.floor(d);
}
distance = d;
}
}