Skip to content

Instantly share code, notes, and snippets.

@petecleary
Created October 17, 2017 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petecleary/0fda5bf43453eab176b783a4407e8c07 to your computer and use it in GitHub Desktop.
Save petecleary/0fda5bf43453eab176b783a4407e8c07 to your computer and use it in GitHub Desktop.
In Browser location test using navigator.geolocation - example @ https://www.piandmash.com/experiments/location-tracking-in-your-browser/
var x = document.getElementById("demo");
var watchID = null;
var counter = 0;
function geo_success(position) {
counter += 1;
x.innerHTML = "Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude + ", Count: " + counter;
}
function geo_error() {
x.innerHTML = "Sorry, no position available.";
}
var geo_options = {
enableHighAccuracy: true,
maximumAge: 30000,
timeout: 27000
};
function toggleGeolocation() {
if ("geolocation" in navigator) {
if (watchID == null) {
watchID = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
} else {
navigator.geolocation.clearWatch(watchID);
watchID = null;
x.innerHTML = "Geolocation stopped.";
}
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment