Skip to content

Instantly share code, notes, and snippets.

@Phoenix2k
Last active October 10, 2016 09:42
Show Gist options
  • Save Phoenix2k/4b0249cc7ce3a1afc6240ae48280e894 to your computer and use it in GitHub Desktop.
Save Phoenix2k/4b0249cc7ce3a1afc6240ae48280e894 to your computer and use it in GitHub Desktop.
JavaScript: Obtain a user's location using Geolocation API
/**
* 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