Last active
October 10, 2016 09:42
-
-
Save Phoenix2k/4b0249cc7ce3a1afc6240ae48280e894 to your computer and use it in GitHub Desktop.
JavaScript: Obtain a user's location using Geolocation API
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
/** | |
* Obtain a user's location | |
* | |
* @see https://developers.google.com/web/fundamentals/native-hardware/user-location/obtain-location | |
* @return { object } Location data | |
*/ | |
window.onload = function() { | |
var startPos; | |
var geoOptions = { enableHighAccuracy : true }; | |
var geoSuccess = function( position ) { | |
startPos = position; | |
console.log( startPos.coords.latitude + ' ' + startPos.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 | |
}; | |
navigator.geolocation.getCurrentPosition( geoSuccess, geoError, geoOptions ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment