Skip to content

Instantly share code, notes, and snippets.

@juristr
Created June 27, 2018 12:58
Show Gist options
  • Save juristr/39c94ebb04fd05adef4c85e217234679 to your computer and use it in GitHub Desktop.
Save juristr/39c94ebb04fd05adef4c85e217234679 to your computer and use it in GitHub Desktop.
Geolocation tests
<html>
<body>
<button id="gpsStart">Start</button>
<button id="gpsStop">Stop</button>
<button id="checkStatus">Status</button>
<div id="log"></div>
<script>
(function () {
var old = console.log;
var logger = document.getElementById('log');
console.log = function (message) {
if (typeof message == 'object') {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
} else {
logger.innerHTML += message + '<br />';
}
}
})();
</script>
<script>
var lastNode = null;
function checkStatus() {
navigator.permissions.query({ name: 'geolocation' }).then(function (result) {
console.log('Status is ' + result.state);
});
}
function geoError(error) {
if (error.code == 1) {
console.log("Permission denied.");
} else if (error.code == 2) {
console.log("Position unavailable.");
} else if (error.code == 3) {
console.log("Timeout.");
} else {
console.log("Unknown error.");
}
}
window.onload = function () {
let watchId;
document.getElementById("gpsStart").addEventListener('click', () => {
watchId = navigator.geolocation.watchPosition(
(position) => {
console.log(`lat: ${position.coords.latitude}, lng: ${position.coords.longitude}, accuracy: ${position.coords.accuracy}`);
},
geoError,
{
timeout: 30000,
maximumAge: 10000,
enableHighAccuracy: true
})
});
document.getElementById("gpsStop").addEventListener('click', () => {
navigator.geolocation.clearWatch(watchId);
alert('stopped');
});
document.getElementById("gpsStop").addEventListener('click', () => {
checkStatus();
});
}</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment