Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Keeping the geolocation on when the application is invisible

Non-standard
This feature is not on a current W3C standards track, but it is supported on the Firefox OS platform. Although implementations may change in the future and it is not supported widely across browsers, it is suitable for use in code dedicated to Firefox OS apps.

This API is available on Firefox OS for internal applications only.

A Firefox OS application may want to keep the Geolocation service running even when invisible. This can be done by request a MozWakeLock and using with watchPosition() when you want to keep your invisible application continuing to use GPS.

Any app, which wants to keep the geolocation service on when the screen is off or the app is background, needs to obtain a wake lock before it is invisible.

To request a wake lock, the method Navigator.requestWakeLock() is called with the 'gps' argument.

var lock = window.navigator.requestWakeLock('gps');

App developers need to be responsible and think carefully about whether they need to keep the geolocation service on ot not. The risk of claiming the lock is that users may forget to close the app when they are done using it, which can result in significant increase in battery use. If you are not certain that obtaining the geolocation lock makes sense in all scenarios, then you should provide the option to enable or disable the geolocation lock option in the app settings.

If you do not release the geolocation wake lock at all, it will be automatically released when the app terminates. However there are some scenarios when you do need to release the lock yourself, for example when user disables the option in settings, or when the app is not performing the task that required the geolocation service to remain on. To release the lock, you call the unlock() method to release the lock:

lock.unlock();

Geolocation wake locks are currently available in both hosted and packaged apps running on the Firefox platform.

Example

var id, target, options;
var wakeLock;

function success(pos) {
  var crd = pos.coords;

  if (target.latitude === crd.latitude && target.longitude === crd.longitude) {
    console.log('Congratulations, you reached the target');
    navigator.geolocation.clearWatch(id);
    wakeLock.unlock();
  }
}

function error(err) {
  console.warn('ERROR(' + err.code + '): ' + err.message);
}

target = {
  latitude : 0,
  longitude: 0
};

options = {
  enableHighAccuracy: false,
  timeout: 5000,
  maximumAge: 0
};

wakeLock = window.navigator.requestWakeLock('gps');
id = navigator.geolocation.watchPosition(success, error, options);

See also

  • Navigator.requestWakeLock(), to take a lock on a resource.
  • MozWakeLock, the interface representing a lock and allowing to manage them, by keeping track of it, and giving the ability to release it.

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, yachiehwu, teoli
 Last updated by: chrisdavidmills,