Skip to content

Instantly share code, notes, and snippets.

@coquin
Created August 18, 2017 06:52
Show Gist options
  • Select an option

  • Save coquin/f1429e2861ecbf3d2cad50e2479b6b9c to your computer and use it in GitHub Desktop.

Select an option

Save coquin/f1429e2861ecbf3d2cad50e2479b6b9c to your computer and use it in GitHub Desktop.
Getting current location of the user
// Note: promise will be rejected if user won't allow browser
// to detect location
function getLocation () {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(resolve, reject)
} else {
reject()
}
})
}
// Usage
getLocation()
.then((pos) => pos.coords)
.catch(() => { default_lat, default_lng })
.then((coords) => doStuff(coords))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment