我正在使用Ionic4
开发必须同时在前台和后台模式下获取用户移动位置的软件。
此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();
});
答案 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/