# Geolocation

Browsers have the ability to track latitude and longitude of devices with GPS capability. Because there are many mobile devices that do not have actual GPS, the API must handle those situations.

When using the navigator.geolocation object and its getCurrentPosition() or watchPosition() methods we need to provide an options object with settings about how to deal with failures. What to do if getting the position takes too long. What to do with a network failure. How long to cache the position results. Whether to try and use an actual GPS call or just use the network or cell tower info to calculate a rough position.

let options = {
  enableHighAccuracy: true, //try to use GPS
  maximumAge: 1000 * 60 * 5, // save results for 5 minutes
  timeout: 20000, // give up after 20 seconds
};
1
2
3
4
5

MDN Geolocation API guide (opens new window)

Back to Hybrid exercises

Last Updated: 1/3/2021, 2:59:25 PM