Skip to content

Commit

Permalink
Create LocationServices.js
Browse files Browse the repository at this point in the history
  • Loading branch information
january-msemakweli authored Sep 9, 2024
1 parent 364c7dc commit da9b39d
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions LocationServices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// JavaScript function for retrieving accurate GPS location (12 decimal places)
function getAccurateLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(onSuccess, onError, {
enableHighAccuracy: true,
maximumAge: 0,
timeout: 5000
});
} else {
console.error("Geolocation is not supported by this browser.");
}

function onSuccess(position) {
// Extract latitude and longitude to 12 decimal places
const latitude = position.coords.latitude.toFixed(12);
const longitude = position.coords.longitude.toFixed(12);

// Send the latitude and longitude back to Shiny (or any server-side process)
Shiny.onInputChange('geolocation', {
lat: latitude,
long: longitude,
time: new Date().toISOString() // Send the timestamp as well
});

// Optionally, log the coordinates for debugging purposes
console.log("Latitude: " + latitude + ", Longitude: " + longitude);
}

function onError(error) {
// Handle any errors in obtaining the location
console.error("Error obtaining location: " + error.message);
Shiny.onInputChange('geolocation', { error: error.message });
}
}

0 comments on commit da9b39d

Please sign in to comment.