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
Want to learn more? Check out our courses