0

我正在使用 Ionic4

开发必须在前台和后台模式下获取用户移动位置的软件。

  1. 我使用了 ionic 4 后台模式来使应用程序在后台运行。
  2. 使用地理位置来获取移动位置。

geolocation.getCurrentPosition在前台运行良好,但在后台运行良好。但是当我们再次恢复应用程序时它会返回值。

我已经尝试了很多方法来解决这个问题,但它仍然给出了相同的结果。谁能帮我解决这个问题?提前致谢。

var options: PositionOptions = { enableHighAccuracy: true };

    this.geolocation.getCurrentPosition(options).then(res => {
      this.zone.run(() => {
        console.log('Get current position Success = ', res);
        this.getCurrentPosistion();
        // this.updateLocationInDatabase(location.coords);
      });
    }).catch(err => {
      console.error('Error occurred = ', err);
      this.getCurrentPosistion();
    });
4

1 回答 1

0

您还可以将 getCurrentLocation 与 watchposition 一起使用,因为 watchposition 跟踪位置。

this.geolocation.getCurrentPosition().then((resp) => {

// resp.coords.latitude

// resp.coords.longitude

}).catch((错误) => {

console.log('获取位置错误', error);

});

让 watch = this.geolocation.watchPosition();

watch.subscribe((数据) => {

// 数据可以是一组坐标,也可以是一个错误(如果发生错误)。

// data.coords.latitude

// data.coords.longitude

});

否则使用插件

https://ionicframework.com/docs/v3/native/background-geolocation/

于 2019-07-05T13:57:55.210 回答