Skip to content

Instantly share code, notes, and snippets.

@CEOehis
Created March 28, 2019 11:41
Show Gist options
  • Save CEOehis/eb72e2c4f27131853ea2ff242cd99da2 to your computer and use it in GitHub Desktop.
Save CEOehis/eb72e2c4f27131853ea2ff242cd99da2 to your computer and use it in GitHub Desktop.
function getCurrentPosition() {
if (navigator.geolocation) {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
} else {
return Promise.reject('geolocation is not available on this device');
}
}
// then to use getCurrentPosition
getCurrentPosition()
.then((position) => {
console.log('position', position); // logs position object https://developer.mozilla.org/en-US/docs/Web/API/Position
})
.catch((error) => {
// error obtaining user location
console.log(error) // geolocation is not available on this device
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment