Last active
January 29, 2018 08:31
-
-
Save snitzr/90ab084416c811ce739698b6969a67b6 to your computer and use it in GitHub Desktop.
Find user location with GPS
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
// 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