Geolocation: If the user denies the position

Remember the permission popup window the browser shows when we call one of the methods to get the position?

If the user denies that, we can intercept this scenario by adding an error handling function, as the second parameter to the methods getCurrentPosition() and watchPosition().

navigator.geolocation.getCurrentPosition(position => {
  console.log(position)
}, error => {
	console.error(error)
})

The object passed to the second parameter contains a code property to distinguish between error types:

  • 1 means permission denied
  • 2 means position unavailable
  • 3 means timeout

Lessons in this unit:

0: Introduction
1: Getting the user's position
2: Watching the position for changes
3: ▶︎ If the user denies the position
4: Adding more options
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. Launching May 21, 2024. Join the waiting list!