-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
364c7dc
commit da9b39d
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |