繁体   English   中英

如何使用Service Worker Angular 6在后台运行watchposition

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

我正在使用Angular 6 PWA,我想检查用户进入或离开特定区域时的位置。 “地理围栏”;

当用户使用我的应用程序时,它可以工作,但是我想要的是即使在浏览器关闭时,也可以在后台执行它。

地理围栏代码

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

不幸的是,当前使用现有的Web API无法实现。 有一个w3c规范,但看起来它已被放弃:

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

要获得这种功能,您将必须构建一个本机应用程序。

不可能。
当浏览器Web应用程序移至后台时,您将不再有权访问位置信息。
也许考虑构建本机应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM