Created
January 4, 2013 10:26
-
-
Save sang4lv/4451534 to your computer and use it in GitHub Desktop.
Geolocation: Everything
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
function gotLocation(position) { | |
console.log("I am at latitude " + position.coords.latitude + ", longitude " + position.coords.longitude + ", and accurate within " + position.coords.accuracy + " meter(s)."); | |
} | |
function handleLocationError(event) { | |
switch(event.errorCode) { | |
case 0: | |
console.log("We have an unknown error..."); | |
break; | |
case 1: | |
console.log("You did not allow me to run"); | |
break; | |
case 2: | |
console.log("Unable to get your position"); | |
break; | |
case 3: | |
console.log("Request timed out"); | |
break; | |
} | |
} | |
if(navigator.geolocation) { | |
console.log("Yep. It's supported"); | |
//Get position once | |
navigator.geolocation.getCurrentPosition(gotLocation, handleLocationError, { | |
enableHighAccuracy: false, //Uses more power, not guaranteed support | |
timeout: 10000 //in milliseconds | |
}); | |
//Get position continuously | |
var continuousWatch = navigator.geolocation.watchPosition(gotLocation, handleLocationError, { | |
enableHighAccuracy: false, | |
timeout: 11000, | |
maximumAge: 30000 //time to get location again | |
}); | |
setTimeout(function() { | |
navigator.geolocation.clearWatch(continuousWatch); | |
}, 60000); | |
} else { | |
console.log("Sorry folks. Geolocation is not yet supported."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment