离子背景地理位置问题

时间:2018-11-14 12:32:31

标签: background ionic2 geolocation tracker

我正在研究离子背景地理位置应用程序。我遇到一个问题,当我从后台关闭应用程序时,后台地理位置服务无法正常工作。我希望如果将应用程序从后台删除,则应用程序后台地理位置服务应该可以正常工作。

有帮助吗?

2 个答案:

答案 0 :(得分:2)

您可以安装后台地理位置插件

$ ionic cordova插件添加cordova-plugin-mauron85-background-geolocation $ npm install --save @ ionic-native / background-geolocation

答案 1 :(得分:1)

您可以安装后台地理位置插件

$ ionic cordova plugin add cordova-plugin-mauron85-background-geolocation
$ npm install --save @ionic-native/background-geolocation

并在.ts中添加它,例如

import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation';

constructor(private backgroundGeolocation: BackgroundGeolocation) { }

...

const config: BackgroundGeolocationConfig = {
            desiredAccuracy: 10,
            stationaryRadius: 20,
            distanceFilter: 30,
            debug: true, //  enable this hear sounds for background-geolocation life-cycle.
            stopOnTerminate: false, // enable this to clear background location settings when the app terminates
    };

this.backgroundGeolocation.configure(config)
  .subscribe((location: BackgroundGeolocationResponse) => {

    console.log(location);

    // IMPORTANT:  You must execute the finish method here to inform the native plugin that you're finished,
    // and the background-task may be completed.  You must do this regardless if your HTTP request is successful or not.
    // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
    this.backgroundGeolocation.finish(); // FOR IOS ONLY

  });

// start recording location
this.backgroundGeolocation.start();

// If you wish to turn OFF background-tracking, call the #stop method.
this.backgroundGeolocation.stop();

有关更多参考,请参见此链接

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