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.

JavaScript
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:

JavaScript
lock.unlock();

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

Example

JavaScript
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.

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/wake_lock_api/keeping_the_geolocation_on_when_the_application_is_invisible

Firefox OS Guide Non-standard Wake Lock API Web