Ionic:使用cordova-plugin-geolocation

时间:2018-03-15 08:26:12

标签: geolocation ionic3 cordova-plugins background-process

我正在尝试使用驱动程序的手机构建一个Android应用程序来跟踪车辆。当驾驶员接到电话或最小化Android手机上的应用程序时,手机会进入后台模式。当后台模式被激活时,我希望设备继续收集位置数据并将其发送到我的服务器。我使用手表(watchPosition)来收集位置数据。应用程序进入后台后,watchPosition似乎没有返回任何位置。 watchPosition是否在后台模式下工作?地理位置在背景模式下是否可行?有关如何解决此问题的任何建议。当调用startTracking()时,流程开始。

startTracking() {
console.log('Gonna Start Tracking');
if (!(this.activeTelematicData && this.activeTelematicData.imei)) {
  alert("Invalid Telematic Data. Please contact support.");
  return undefined;
}

this.active = true;
this.watchAndSendLocationData();
this.backgroundMode.enable();
this.backgroundMode.on("activate").subscribe(() => {
  console.log("background mode set.");
  this.watchAndSendLocationData();
});
// this.collectAndSendLocationData();
// this.process = setInterval(this.collectAndSendLocationData, 10000);}

watchAndSendLocationData() {
this.process = this.geolocation.watchPosition(this.geolocationOptions);
this.process
  // .filter((p) => p.coords !== undefined) //Filter Out Errors
  .subscribe((position) => {
    if (this.active && position && position.coords && position.timestamp) {
      this.sendLocationData(position);
    } else if (this.active) {
      // alert("Position empty: " + JSON.stringify(position.timestamp) + ". Please contact support.");
    }
  }, (error) => {
    alert("Get Location Error message: " + error.message);
    alert("Get Location Error: " + JSON.stringify(error));
  });}

P.S。 backgroundMode.on(“activate”)在接收到日志语句时处于激活状态。我知道有更多的Cordova插件可以直接解决背景地理位置问题,但我正在尝试使用这个插件来解决它,原因很简单。

0 个答案:

没有答案