使用Cordova BackgroundGeolocation插件实时观察位置

时间:2017-12-15 16:04:44

标签: cordova ionic-framework cordova-plugins

我正在使用Ionic和Cordova插件。我有一个适用于Cordova Background-mode和Geolocation插件的应用程序:

this.backgroundMode.enable();

this.backgroundMode.on("activate").subscribe(() => {
    this.backgroundMode.disableWebViewOptimizations(); // For GPS tracking at background
});

let watch = this.geolocation.watchPosition({enableHighAccuracy: true});
watch.subscribe((resp) => {
    // Use resp.latitude and resp.longitude
});

直到我意识到存在BackgroundGeolocation plugin,在许多用户看来,这比使用两个插件要好得多,速度快。

我的问题是我不知道如何使用此插件复制旧代码,我试过:

const config: BackgroundGeolocationConfig = {
    desiredAccuracy: 0, // Best accuracy possible
    stationaryRadius: 0,
    distanceFilter: 0
};

this.backgroundGeolocation.configure(config).subscribe((location: BackgroundGeolocationResponse) => {
    // Use location.latitude and location.longitude,
});

但是这个解决方案并不像第一个那样“实时”。

提前致谢并对我的英语表示抱歉

2 个答案:

答案 0 :(得分:1)

经过几天的搜索和尝试,我发现我们应该在BackgroundGeolocation之后使用Geolocation。它完全按预期工作。

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

this.backgroundGeolocation.configure(config)
  .then(() => {

    this.backgroundGeolocation.on(BackgroundGeolocationEvents.location).subscribe((location: BackgroundGeolocationResponse) => {
      console.log(location);
      this.http.post('http://riyadkhalifeh.com/insert.php', { log: location.latitude, lat: location.longitude }, {})
        .then(data => {

          console.log(data.status);
          console.log(data.data); // data received by server
          console.log(data.headers);

        })
        .catch(error => {

          console.log(error);
          console.log(error); // error message as string
          console.log(error);

        });
    });

  });

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

//////////////
this.geolocation.getCurrentPosition().then((resp) => {
  this.http.post(url, { lat: resp.coords.latitude, long: resp.coords.longitude }, {})
    .then(data => {

      console.log(data.status);
      console.log(data.data); // data received by server
      console.log(data.headers);

    })
    .catch(error => {

      console.log(error);
      console.log(error); // error message as string
      console.log(error);

    });
}).catch((error) => {
  console.log('Error getting location', error);
});

let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {

  this.http.post(url, { lat: data.coords.latitude, long: data.coords.longitude }, {})
    .then(data => {

      console.log(data.status);
      console.log(data.data); // data received by server
      console.log(data.headers);

    })
    .catch(error => {

      console.log(error);
      console.log(error); // error message as string
      console.log(error);

    });
});

祝大家好运

答案 1 :(得分:0)

这应该有效,如果它是实时的,但.configure是一个可观察的,只有当坐标发生变化时才会听。