AI Workshop: learn to build apps with AI →
Geolocation: Adding more options

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


As mentioned earlier, one error type is timeout. Getting the position can take time; you can set a maximum time for the operation via the options object.

You can set a timeout by passing a third parameter—an options object—to getCurrentPosition() and watchPosition().

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

})

This options object can contain the following properties:

  • timeout — maximum time in milliseconds before the request fails
  • maximumAge — maximum age in milliseconds of a cached position to use; older cached positions are ignored
  • enableHighAccuracy — boolean (default false); when true, requests the most accurate position available, which may take longer and use more power

Example usage:

navigator.geolocation.getCurrentPosition(position => {
  console.log(position)
}, error => {
	console.error(error)
}, {
  timeout: 1000,
  maximumAge: 10000,
  enableHighAccuracy: true
})

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