-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAI_GPS_Maps_RealTime.html
More file actions
269 lines (254 loc) · 11.4 KB
/
Copy pathAI_GPS_Maps_RealTime.html
File metadata and controls
269 lines (254 loc) · 11.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GPS Satellite Map with ZIP Code Search</title>
<!-- Leaflet CSS: The stylesheet for the mapping library -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
<!-- Tailwind CSS for modern styling -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Custom CSS: To style the map and search input -->
<style>
html, body, #map {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
font-family: 'Inter', sans-serif;
overflow: hidden;
}
#map p {
text-align: center;
padding: 2rem;
font-size: 1.2rem;
color: #555;
}
#search-container {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
z-index: 1000;
background: rgba(255, 255, 255, 0.9);
padding: 8px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.2);
display: flex;
align-items: center;
gap: 10px;
}
#zip-input {
padding: 8px;
font-size: 1rem;
width: 250px;
border: 1px solid #ccc;
border-radius: 4px;
}
.control-button {
background-color: #3b82f6;
color: white;
padding: 8px 12px;
border-radius: 8px;
font-size: 0.9rem;
cursor: pointer;
border: none;
transition: all 0.2s ease-in-out;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.control-button:hover {
background-color: #2563eb;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
#timer-display {
font-size: 1rem;
font-weight: bold;
color: #333;
min-width: 80px;
text-align: center;
}
</style>
<!-- Google Fonts for a cleaner look -->
<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=Inter:wght@400;500;700&display=swap" rel="stylesheet">
</head>
<body class="bg-gray-100">
<!-- Search input and button container -->
<div id="search-container">
<input id="zip-input" type="text" placeholder="Enter ZIP code prefix (e.g., 55)">
<button id="current-location-button" class="control-button">Show Current Location</button>
<button id="gps-timer-button" class="control-button">Start GPS Timer</button>
<div id="timer-display"></div>
</div>
<!-- Map container -->
<div id="map">
<p>Attempting to locate you...</p>
</div>
<!-- Leaflet JavaScript: The core mapping library -->
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>
<!-- Esri Leaflet JavaScript: Plugin for satellite basemaps -->
<script src="https://unpkg.com/esri-leaflet@3.0.10/dist/esri-leaflet.js"></script>
<!-- Main application logic -->
<script>
document.addEventListener('DOMContentLoaded', () => {
const mapContainer = document.getElementById('map');
const zipInput = document.getElementById('zip-input');
const currentLocationButton = document.getElementById('current-location-button');
const gpsTimerButton = document.getElementById('gps-timer-button');
const timerDisplay = document.getElementById('timer-display');
let map, userMarker, searchTimeout, timerInterval;
let timerRunning = false;
// This function creates and returns a new map instance.
function initializeMap(lat, lng, zoomLevel = 10) {
// Remove the initial "locating..." message
mapContainer.innerHTML = "";
// Create the map instance and set the view
const newMap = L.map('map').setView([lat, lng], zoomLevel);
// Add the satellite and label layers
L.esri.basemapLayer('Imagery').addTo(newMap);
L.esri.basemapLayer('ImageryLabels').addTo(newMap);
return newMap;
}
// Always initialize the map first with a default view (center of the US)
map = initializeMap(39.8283, -98.5795);
// Geolocation handling
function locateUser(zoomLevel) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => onSuccess(position, zoomLevel),
onError, {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
}
);
} else {
L.popup()
.setLatLng(map.getCenter())
.setContent('<p>Sorry, Geolocation is not supported by your browser.</p>')
.openOn(map);
}
}
function onSuccess(position, zoomLevel) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// Store the coordinates for the timer functionality
const locationData = { latitude, longitude };
localStorage.setItem('lastLocation', JSON.stringify(locationData));
// Clear any previous markers
if (userMarker) {
map.removeLayer(userMarker);
}
map.setView([latitude, longitude], zoomLevel);
userMarker = L.marker([latitude, longitude]).addTo(map);
userMarker.bindPopup(`<b>You are here!</b><br>Your approximate location.<br>Lat: ${latitude.toFixed(4)}, Lng: ${longitude.toFixed(4)}`).openPopup();
}
function onError(err) {
let errorMessage = 'An unknown error occurred.';
switch (err.code) {
case err.PERMISSION_DENIED:
errorMessage = 'Location access denied. Please enable it or use ZIP code search.';
break;
case err.POSITION_UNAVAILABLE:
errorMessage = 'Location information is unavailable.';
break;
case err.TIMEOUT:
errorMessage = 'Location request timed out.';
break;
}
// Display the error message in a temporary popup
L.popup()
.setLatLng(map.getCenter())
.setContent(`<p><strong>Error:</strong> ${errorMessage}</p>`)
.openOn(map);
}
// ZIP code search functionality with Nominatim API
zipInput.addEventListener('input', () => {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => {
const query = zipInput.value.trim();
if (userMarker) {
map.removeLayer(userMarker);
userMarker = null;
}
if (query.length >= 2) {
const url = `https://nominatim.openstreetmap.org/search?q=${query}&format=json&addressdetails=1&limit=1`;
fetch(url)
.then(response => response.json())
.then(data => {
if (data && data.length > 0) {
const result = data[0];
const lat = parseFloat(result.lat);
const lng = parseFloat(result.lon);
map.setView([lat, lng], 12);
userMarker = L.marker([lat, lng]).addTo(map);
userMarker.bindPopup(`<b>Search Result:</b><br>${result.display_name}`).openPopup();
const locationData = { latitude: lat, longitude: lng, isZip: true };
localStorage.setItem('lastLocation', JSON.stringify(locationData));
} else {
L.popup()
.setLatLng(map.getCenter())
.setContent(`<p><strong>Error:</strong> No location found for that query. Try a more specific one.</p>`)
.openOn(map);
}
})
.catch(error => {
console.error("Geocoding error:", error);
L.popup()
.setLatLng(map.getCenter())
.setContent(`<p><strong>Error:</strong> An error occurred while searching.</p>`)
.openOn(map);
});
}
}, 300);
});
// "Show Current Location" button click handler
currentLocationButton.addEventListener('click', () => {
locateUser(16); // Set a higher zoom level for user location
});
// "Start GPS Timer" button click handler
gpsTimerButton.addEventListener('click', () => {
if (!timerRunning) {
startTimer();
} else {
stopTimer();
}
});
function startTimer() {
timerRunning = true;
gpsTimerButton.textContent = 'Stop GPS Timer';
updateTimerDisplay(10);
timerInterval = setInterval(() => {
let timeLeft = parseInt(timerDisplay.textContent.split('|')[1].trim(), 10);
if (timeLeft > 0) {
updateTimerDisplay(timeLeft - 1);
} else {
locateUser(16);
updateTimerDisplay(10);
}
}, 1000);
}
function stopTimer() {
timerRunning = false;
gpsTimerButton.textContent = 'Start GPS Timer';
clearInterval(timerInterval);
timerDisplay.textContent = '';
}
function updateTimerDisplay(seconds) {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const sec = String(now.getSeconds()).padStart(2, '0');
timerDisplay.textContent = `${hours}:${minutes}:${sec} | ${seconds}s`;
}
// Initial location request on page load
locateUser(10);
});
</script>
</body>
</html>