Skip to content

Instantly share code, notes, and snippets.

@snitzr
Last active January 29, 2018 08:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snitzr/90ab084416c811ce739698b6969a67b6 to your computer and use it in GitHub Desktop.
Save snitzr/90ab084416c811ce739698b6969a67b6 to your computer and use it in GitHub Desktop.
Find user location with GPS
// adapted and simplified from
// https://developers.google.com/web/fundamentals/native-hardware/user-location/obtain-location
// find location
window.onload = function() {
var geoOptions = {
timeout: 10000, // milliseconds
maximumAge: 5 * 60 * 1000 // milliseconds
}
var geoSuccess = function(position) {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
};
var geoError = function(error) {
console.log('Error occurred. Error code: ' + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from location provider)
// 3: timed out
};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment