51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
// assets/js/location.js
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Check if the current page is a staff page and if geolocation is supported
|
|
if (window.location.search.includes('page=staff')) {
|
|
if ("geolocation" in navigator) {
|
|
console.log("Geolocation supported. Starting GPS tracking...");
|
|
|
|
// Watch position updates continuously
|
|
navigator.geolocation.watchPosition(
|
|
function(position) {
|
|
const lat = position.coords.latitude;
|
|
const lng = position.coords.longitude;
|
|
|
|
// Send to server
|
|
sendLocationToServer(lat, lng);
|
|
},
|
|
function(error) {
|
|
console.error("Error getting location: ", error);
|
|
},
|
|
{
|
|
enableHighAccuracy: true,
|
|
maximumAge: 10000, // 10 seconds
|
|
timeout: 5000
|
|
}
|
|
);
|
|
} else {
|
|
console.warn("Geolocation is not supported by this browser.");
|
|
}
|
|
}
|
|
});
|
|
|
|
function sendLocationToServer(lat, lng) {
|
|
const formData = new FormData();
|
|
formData.append('action', 'update_location');
|
|
formData.append('lat', lat);
|
|
formData.append('lng', lng);
|
|
|
|
fetch('api/location.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
// Silently succeed
|
|
// console.log("Location updated", data);
|
|
})
|
|
.catch(error => {
|
|
console.error('Error updating location:', error);
|
|
});
|
|
}
|