背景地理定位不是可观察的

时间:2019-01-31 01:12:41

标签: cordova ionic-framework geolocation ionic3

我需要在我的应用中实施后台地理位置。我访问了ionic documentation,下面显示了代码:

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

  });

当我在应用中使用此代码时,ts-lint指责configure(config)方法是Promise<any>而不是Observable,因此,我不能使用subscribe 。我将subscribe切换为then。但是当我运行时,在下面显示此错误:

ERROR Error: Uncaught (in promise): TypeError: Object(...) is not a function
TypeError: Object(...) is not a function
    at BackgroundGeolocation.configure (vendor.js:82333)
// error below ommited

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

问题是插件版本。在Ionic 3中,此版本可以正常工作:

config.xml文件: <plugin name="cordova-plugin-mauron85-background-geolocation" spec="^2.2.5" />

package.json文件: "@ionic-native/background-geolocation": "^3.14.0",

我在此repository中找到了这个答案。

Ionic v3 documentation中是错误的,因为那里说我们需要使用以下命令:

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

但是我们需要使用以下命令才能正常工作,最后使用subscribe

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