Ionic4:如果我们最小化应用程序,则无法获得位置

时间:2019-07-04 10:29:39

标签: cordova ionic-framework phonegap ionic-plugins

我正在使用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();
    });

1 个答案:

答案 0 :(得分:0)

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

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

// resp.coords.latitude

// resp.coords.longitude

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

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

});

让手表= this.geolocation.watchPosition();

watch.subscribe((data)=> {

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

// data.coords.latitude

// data.coords.longitude

});

否则请使用插件

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