-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
187 lines (164 loc) · 7.6 KB
/
index.html
File metadata and controls
187 lines (164 loc) · 7.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GPS Speedometer</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400..900&display=swap" rel="stylesheet">
<style>
/* Custom styles for the speedometer */
body {
font-family: 'Orbitron', sans-serif;
touch-action: manipulation; /* Prevents double-tap zoom on mobile */
}
.speed-display {
text-shadow: 0 0 10px rgba(52, 211, 153, 0.5), 0 0 20px rgba(52, 211, 153, 0.5);
}
.unit-display {
text-shadow: 0 0 5px rgba(52, 211, 153, 0.4);
}
/* Custom styles for active/inactive buttons */
.unit-btn.active {
background-color: #10B981; /* emerald-500 */
color: #052e16; /* dark green */
box-shadow: 0 0 15px rgba(16, 185, 129, 0.6);
}
.unit-btn.inactive {
background-color: #374151; /* gray-700 */
color: #D1D5DB; /* gray-300 */
}
</style>
</head>
<body class="bg-gray-900 text-white flex items-center justify-center min-h-screen">
<div id="app" class="w-full max-w-md mx-auto p-4 sm:p-6 md:p-8 text-center bg-gray-800 rounded-3xl shadow-2xl border-2 border-gray-700">
<!-- App Title -->
<h1 class="text-2xl sm:text-3xl font-bold text-gray-300 mb-6">GPS Speedometer</h1>
<!-- Speedometer Display -->
<div class="relative w-72 h-72 sm:w-80 sm:h-80 mx-auto bg-gray-900 rounded-full flex flex-col items-center justify-center p-4 border-8 border-gray-700 shadow-inner">
<div id="speed" class="speed-display text-7xl sm:text-8xl font-black text-emerald-400">---</div>
<div id="unit" class="unit-display text-2xl sm:text-3xl font-bold text-emerald-300 mt-2">km/h</div>
</div>
<!-- Unit Selection Controls -->
<div class="mt-8 flex justify-center space-x-4">
<button id="kphBtn" class="unit-btn active text-lg font-semibold py-3 px-8 rounded-full transition-all duration-300 ease-in-out transform hover:scale-105">
KM/H
</button>
<button id="mphBtn" class="unit-btn inactive text-lg font-semibold py-3 px-8 rounded-full transition-all duration-300 ease-in-out transform hover:scale-105">
MPH
</button>
</div>
<!-- Status and Accuracy Information -->
<div class="mt-6 text-gray-400">
<p id="status">Requesting location access...</p>
<p id="accuracy" class="text-sm mt-2"></p>
</div>
</div>
<script>
// DOM element references
const speedElement = document.getElementById('speed');
const unitElement = document.getElementById('unit');
const statusElement = document.getElementById('status');
const accuracyElement = document.getElementById('accuracy');
const kphBtn = document.getElementById('kphBtn');
const mphBtn = document.getElementById('mphBtn');
// Application state
let currentUnit = 'kph'; // 'kph' or 'mph'
let lastSpeedMps = 0; // Last speed received in meters per second
// --- Event Listeners ---
kphBtn.addEventListener('click', () => setUnit('kph'));
mphBtn.addEventListener('click', () => setUnit('mph'));
/**
* Sets the speed unit and updates the UI accordingly.
* @param {string} unit - The unit to switch to ('kph' or 'mph').
*/
function setUnit(unit) {
currentUnit = unit;
unitElement.textContent = unit === 'kph' ? 'km/h' : 'mph';
// Update button styles
kphBtn.classList.toggle('active', unit === 'kph');
kphBtn.classList.toggle('inactive', unit !== 'kph');
mphBtn.classList.toggle('active', unit === 'mph');
mphBtn.classList.toggle('inactive', unit !== 'mph');
// Recalculate and display speed with the new unit
updateSpeedDisplay(lastSpeedMps);
}
/**
* Converts speed from m/s and updates the display.
* @param {number | null} speedMps - Speed in meters per second.
*/
function updateSpeedDisplay(speedMps) {
if (speedMps === null || isNaN(speedMps)) {
speedElement.textContent = '---';
return;
}
let displaySpeed = 0;
if (currentUnit === 'kph') {
// Convert m/s to km/h (1 m/s = 3.6 km/h)
displaySpeed = speedMps * 3.6;
} else {
// Convert m/s to mph (1 m/s = 2.23694 mph)
displaySpeed = speedMps * 2.23694;
}
// Display speed rounded to the nearest integer
speedElement.textContent = Math.round(displaySpeed);
}
/**
* Success callback for the Geolocation API.
* @param {GeolocationPosition} position - The position object from the API.
*/
function onLocationUpdate(position) {
statusElement.textContent = 'GPS signal acquired.';
const coords = position.coords;
// The speed property is in meters per second.
lastSpeedMps = coords.speed === null ? 0 : coords.speed;
updateSpeedDisplay(lastSpeedMps);
// Display accuracy if available
if (coords.accuracy) {
accuracyElement.textContent = `Accuracy: ${coords.accuracy.toFixed(1)} meters`;
}
}
/**
* Error callback for the Geolocation API.
* @param {GeolocationPositionError} error - The error object from the API.
*/
function onLocationError(error) {
console.error('Geolocation error:', error.message);
switch (error.code) {
case error.PERMISSION_DENIED:
statusElement.textContent = "Location access denied.";
break;
case error.POSITION_UNAVAILABLE:
statusElement.textContent = "Location information is unavailable.";
break;
case error.TIMEOUT:
statusElement.textContent = "The request to get user location timed out.";
break;
default:
statusElement.textContent = "An unknown error occurred.";
break;
}
speedElement.textContent = 'ERR';
accuracyElement.textContent = '';
}
// --- Main execution ---
// Check if Geolocation is supported by the browser
if ('geolocation' in navigator) {
statusElement.textContent = "Waiting for GPS signal...";
// Options for the geolocation watch
const geoOptions = {
enableHighAccuracy: true, // Request the most accurate position
maximumAge: 0, // Don't use a cached position
timeout: 27000 // Time in ms before the error callback is invoked
};
// Start watching the user's position
navigator.geolocation.watchPosition(onLocationUpdate, onLocationError, geoOptions);
} else {
statusElement.textContent = "Geolocation is not supported by your browser.";
speedElement.textContent = 'N/A';
}
</script>
</body>
</html>