Dev
Dev

Reputation: 125

How can I run watchposition on background using service worker Angular 6

I'm using Angular 6 PWA , I want to check the position of the user when he enters or leaves a particular area. "geoFencing";

It's works when the user is on my app, but what I want is to execute it in the background even when the browser is closed.

Code for the geofencing

var id, target, options;

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);
  }
}

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

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

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

id = navigator.geolocation.watchPosition(success, error, options);

Upvotes: 1

Views: 1543

Answers (2)

Peter Fernandes
Peter Fernandes

Reputation: 316

Unfortunately, this is not currently possible with existing web APIs. There is a w3c spec, but it looks like it has been abandoned:

https://www.w3.org/TR/geofencing/

To get this sort of functionality you will have to build a native app.

Upvotes: 3

Mathias
Mathias

Reputation: 4589

Not possible.
When the browser web app moves to the background you no longer have access to location information.
Perhaps consider building a native application.

Upvotes: 2

Related Questions