背景地理位置离子 3 未更新



离子的背景地理位置插件没有更新。我想要的功能是每 30 秒向插件询问纬度 lng 值(如果可用(。问题是,它最初只给我值,然后背景停止。前景很好,真的是背景。基本上,我无法在后台进行第一次初始发送后发送请求。

全球定位系统(gps.ts(

startTracking() {
  console.log("started tracking");
  const config: BackgroundGeolocationConfig = {
    desiredAccuracy: 10,
    stationaryRadius: 20,
    distanceFilter: 30,
    debug: false, //  enable this hear sounds for background-geolocation life-cycle.
    stopOnTerminate: false
  };
  this.backgroundGeolocation
    .configure(config)
    .subscribe((location: BackgroundGeolocationResponse) => {
      this.zone.run(() => {
        this.lat = location.latitude;
        this.lng = location.longitude;
        this.bearing = location.bearing;
        this.speed = location.speed;
        this.accuracy = location.accuracy;
        this.timestamp = location.time;
      });
      this.backgroundGeolocation.finish(); // FOR IOS ONLY
      this.backgroundGeolocation.stop();
    });
  this.backgroundGeolocation.start();
}
sendGPS() {
  this.optionsService.sendGPS(gpsData).subscribe(result => {});
}
stopTracking() {
  this.sendGPS();
}

app.component.ts

constructor() {
  this.sendGPSStart();
  this.interval();
}
sendGPSStart() {
  this.locationTracker.startTracking();
}
sendGPSStop() {
  this.locationTracker.stopTracking();
}
interval() {
  setInterval(() => {
    this.sendGPSStart();
    this.sendGPSStop();
  }, "30000");
}

查看示例,例如dnchia/Ionic3-Background-Geolocation,您将在后台配置间隔以及定期前台发送

全球定位系统(gps.ts(

startTracking(interval) {
    console.log('started tracking')
    const config: BackgroundGeolocationConfig = {
      desiredAccuracy: 10,
      stationaryRadius: 20,
      distanceFilter: 30,
      debug: false, //  enable this hear sounds for background-geolocation life-cycle.
      stopOnTerminate: false,
      interval: interval
    };

app.component.ts

interval = 30000;
constructor() {
  this.sendGPSStart()
  this.interval()
}
sendGPSStart(){
  this.locationTracker.startTracking(this.interval);
}
sendGPSStop(){
  this.locationTracker.stopTracking();
}
interval() {
  setInterval(() => {
    this.locationTracker.sendGPS();
  }, this.interval)
}
目前在

项目github上有关于此的错误报告:

看:https://github.com/transistorsoft/cordova-background-geolocation-lt/issues/548和https://github.com/transistorsoft/cordova-background-geolocation-lt/issues/539

该项目的创建者建议删除启动画面插件(cordova-plugin-splashscreen(

我正在使用该插件,经过一些阅读,我相信 beaviour

首先,您需要删除该行

this.backgroundGeolocation.stop()

从代码。

其次,由于IOS(和Android(限制,后台地理位置插件不会随时执行,而仅在"Antena发生更改事件"时执行 因此,您不仅需要真正移动,因为您需要连接和断开与载波天线的连接,请尝试应用我说的此更改,打开调试,然后乘车进行测试

在配置中,必须确定以毫秒为单位的间隔。

const config: BackgroundGeolocationConfig = {
  desiredAccuracy: 10,
  stationaryRadius: 20,
  distanceFilter: 30,
  debug: false, //  enable this hear sounds for background-geolocation life-cycle.
  stopOnTerminate: false,
  interval: 2000 // <= example 2 seconds interval
};

然后在订阅部分做一个 ajax 调用,比如

    this.backgroundGeolocation.on(BackgroundGeolocationEvents.location).subscribe((location: BackgroundGeolocationResponse) => {
          console.log(location);
          this.postData = {
            user_id: this.userId,
            latitude: location.latitude,
            longitude: location.longitude,
          };
          console.log(location);
          this.http.post<ActivationResponse>(environment.crmUrl + '/api/start-job', this.postData,
            { headers: new HttpHeaders({ Authorization: this.token }), observe: 'response' })
            .subscribe(async (response: HttpResponse<ActivationResponse>) => {
              console.log(response);
              this.backgroundGeolocation.finish(); // FOR IOS ONLY
            }, async (errorResponse: HttpErrorResponse) => {
              console.log(errorResponse);
            });
});

它将每 2 秒自动将新的 GPS 位置发送到您确定的服务器。

当postTemplate是jsonObject和jsonArray的数组数组时,所有位置(即使是单个位置(都将作为对象数组发送!

在服务器端,我将 JSON 对象更改为对象数组。

例如

{
  "user_id": "1",
  "lon": "13.2",
  "lat": "82.3"
}

我将上面的 JSON 对象更改为以下内容

[{
  "user_id": "1",
  "lon": "13.2",
  "lat": "82.3"
}]

相关内容

  • 没有找到相关文章

最新更新