let map;
let marker;
let locations = [Link]([Link]('locations')) || [];
function initMap() {
// Initialize map centered on a default location (e.g., New York)
map = new [Link]([Link]('map'), {
center: { lat: 40.7128, lng: -74.0060 },
zoom: 10
});
}
function trackLocation() {
if ([Link]) {
[Link](showPosition, showError, {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
const lat = [Link];
const lng = [Link];
const timestamp = new Date().toLocaleString();
// Update map and marker
const pos = { lat, lng };
[Link](pos);
if (marker) [Link](null); // Remove old marker
marker = new [Link]({
position: pos,
map: map,
title: `Location at ${timestamp}`
});
// Display info
[Link]('locationInfo').innerHTML = `
<p><strong>Latitude:</strong> ${lat}</p>
<p><strong>Longitude:</strong> ${lng}</p>
<p><strong>Timestamp:</strong> ${timestamp}</p>
<p><strong>Accuracy:</strong> ${[Link]} meters</p>
`;
// Store in local storage
[Link]({ lat, lng, timestamp });
[Link]('locations', [Link](locations));
}
function showError(error) {
switch([Link]) {
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.");
break;
case [Link]:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.");
break;
}
}
// Event listener for button
[Link]('trackBtn').addEventListener('click', trackLocation);
// Initialize map on load
[Link] = initMap;