-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
289 lines (244 loc) · 8.73 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
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
// Elementos do DOM
const screens = {
start: document.getElementById('start-screen'),
running: document.getElementById('running-screen')
};
const elements = {
distance: document.getElementById('distance'),
time: document.getElementById('time'),
pace: document.getElementById('pace'),
map: document.getElementById('map'),
runHistory: document.getElementById('run-history')
};
const buttons = {
start: document.getElementById('start-run'),
pause: document.getElementById('pause-run'),
resume: document.getElementById('resume-run'),
finish: document.getElementById('finish-run')
};
// Estado da aplicação
const state = {
isRunning: false,
isPaused: false,
startTime: null,
pausedTime: 0,
totalPausedTime: 0,
pauseStartTime: null,
totalDistance: 0,
previousPosition: null,
watchId: null,
map: null,
path: null,
timerInterval: null,
runs: []
};
// Verificação de permissão de geolocalização
async function checkGeolocationPermission() {
if (!('geolocation' in navigator)) {
showGeolocationError('Seu navegador não suporta geolocalização.');
return false;
}
try {
const permission = await navigator.permissions.query({ name: 'geolocation' });
if (permission.state === 'denied') {
showGeolocationError('Permissão de localização negada. Por favor, permita o acesso à sua localização nas configurações do navegador.');
return false;
}
return true;
} catch (error) {
// Fallback para navegadores que não suportam permissions API
return true;
}
}
function showGeolocationError(message) {
const errorHtml = `
<div class="alert alert-warning alert-dismissible fade show" role="alert">
${message}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
const errorContainer = document.createElement('div');
errorContainer.innerHTML = errorHtml;
elements.runHistory.insertAdjacentElement('beforebegin', errorContainer.firstChild);
}
// Inicialização do mapa
function initializeMap() {
state.map = L.map(elements.map, {
zoomControl: false,
attributionControl: false
});
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(state.map);
state.path = L.polyline([], { color: '#0d6efd', weight: 4 }).addTo(state.map);
}
// Gerenciamento de telas
function showScreen(screenName) {
Object.values(screens).forEach(screen => screen.classList.remove('active'));
screens[screenName].classList.add('active');
}
// Funções de tempo
function formatTime(ms) {
const totalSeconds = Math.floor(ms / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
}
function pad(num) {
return num.toString().padStart(2, '0');
}
function updateTimer() {
if (!state.isRunning || state.isPaused) return;
const currentTime = Date.now();
const elapsedTime = currentTime - state.startTime - state.totalPausedTime;
elements.time.textContent = formatTime(elapsedTime);
updatePace(elapsedTime);
}
function updatePace(elapsedTime) {
if (state.totalDistance === 0) {
elements.pace.textContent = '0:00';
return;
}
const paceInMinutesPerKm = (elapsedTime / 60000) / state.totalDistance;
const paceMinutes = Math.floor(paceInMinutesPerKm);
const paceSeconds = Math.floor((paceInMinutesPerKm - paceMinutes) * 60);
elements.pace.textContent = `${paceMinutes}:${pad(paceSeconds)}`;
}
// Gerenciamento de posição
async function startPositionTracking() {
const hasPermission = await checkGeolocationPermission();
if (!hasPermission) return;
if ('geolocation' in navigator) {
state.watchId = navigator.geolocation.watchPosition(
updatePosition,
handleGeolocationError,
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
);
}
}
function updatePosition(position) {
if (!state.isRunning || state.isPaused) return;
const { latitude, longitude } = position.coords;
const currentPosition = [latitude, longitude];
if (state.previousPosition) {
const distance = calculateDistance(state.previousPosition, currentPosition);
state.totalDistance += distance;
elements.distance.textContent = state.totalDistance.toFixed(2);
} else {
state.map.setView(currentPosition, 16);
}
state.path.addLatLng(currentPosition);
state.previousPosition = currentPosition;
state.map.panTo(currentPosition);
}
function calculateDistance(pos1, pos2) {
const R = 6371; // Raio da Terra em km
const [lat1, lon1] = pos1;
const [lat2, lon2] = pos2;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}
function toRad(degrees) {
return degrees * Math.PI / 180;
}
// Ações da corrida
async function startRun() {
const hasPermission = await checkGeolocationPermission();
if (!hasPermission) return;
state.isRunning = true;
state.startTime = Date.now();
state.totalPausedTime = 0;
state.totalDistance = 0;
state.previousPosition = null;
showScreen('running');
startPositionTracking();
state.timerInterval = setInterval(updateTimer, 1000);
}
function pauseRun() {
state.isPaused = true;
state.pauseStartTime = Date.now();
buttons.pause.classList.add('d-none');
buttons.resume.classList.remove('d-none');
}
function resumeRun() {
state.isPaused = false;
state.totalPausedTime += Date.now() - state.pauseStartTime;
buttons.resume.classList.add('d-none');
buttons.pause.classList.remove('d-none');
}
function finishRun() {
const currentTime = Date.now();
const elapsedTime = currentTime - state.startTime - state.totalPausedTime;
const runData = {
distance: state.totalDistance.toFixed(2),
time: formatTime(elapsedTime),
pace: elements.pace.textContent,
date: new Date().toLocaleString()
};
state.runs.unshift(runData);
updateRunHistory();
state.isRunning = false;
clearInterval(state.timerInterval);
navigator.geolocation.clearWatch(state.watchId);
showScreen('start');
}
function updateRunHistory() {
elements.runHistory.innerHTML = state.runs.map(run => `
<div class="card shadow-sm mb-3">
<div class="card-body">
<div class="row text-center">
<div class="col-4">
<div class="stat-label">Distância</div>
<div class="stat-value">${run.distance}</div>
<div class="stat-unit">km</div>
</div>
<div class="col-4">
<div class="stat-label">Tempo</div>
<div class="stat-value">${run.time}</div>
</div>
<div class="col-4">
<div class="stat-label">Pace</div>
<div class="stat-value">${run.pace}</div>
<div class="stat-unit">min/km</div>
</div>
</div>
<div class="text-muted text-center mt-2">
<small>${run.date}</small>
</div>
</div>
</div>
`).join('');
}
function handleGeolocationError(error) {
let message = 'Erro ao acessar sua localização.';
switch (error.code) {
case error.PERMISSION_DENIED:
message = 'Permissão de localização negada. Por favor, permita o acesso à sua localização nas configurações do navegador.';
break;
case error.POSITION_UNAVAILABLE:
message = 'Informações de localização indisponíveis. Verifique sua conexão GPS.';
break;
case error.TIMEOUT:
message = 'Tempo excedido ao tentar obter sua localização. Tente novamente.';
break;
}
showGeolocationError(message);
if (state.isRunning) {
finishRun();
}
}
// Event Listeners
buttons.start.addEventListener('click', startRun);
buttons.pause.addEventListener('click', pauseRun);
buttons.resume.addEventListener('click', resumeRun);
buttons.finish.addEventListener('click', finishRun);
// Inicialização
initializeMap();