Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created August 1, 2017 13:20
Show Gist options
  • Save prof3ssorSt3v3/a2cfe0f8d77bb204230f541fb7508873 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/a2cfe0f8d77bb204230f541fb7508873 to your computer and use it in GitHub Desktop.
//geolocation.js
// How to use Navigator.geolocation
//
let G, options, spans;
document.addEventListener('DOMContentLoaded', init);
function init(){
if(navigator.geolocation){
let giveUp = 1000 * 30; //30 seconds
let tooOld = 1000 * 60 * 60; //one hour
options ={
enableHighAccuracy: true,
timeout: giveUp,
maximumAge: tooOld
}
navigator.geolocation.getCurrentPosition(gotPos, posFail, options);
}else{
//using an old browser that doesn't support geolocation
}
}
function gotPos(position){
spans = document.querySelectorAll('p span');
spans[0].textContent = position.coords.latitude;
spans[1].textContent = position.coords.longitude;
spans[2].textContent = position.coords.accuracy;
spans[6].textContent = position.timestamp;
}
function posFail(err){
//err is a number
let errors = {
1: 'No permission',
2: 'Unable to determine',
3: 'Took too long'
}
document.querySelector('h1').textContent = errors[err];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment