Created
October 17, 2017 04:57
-
-
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/
This file contains hidden or 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
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