Backgroundgeolocation在后台不起作用

时间:2019-03-31 06:38:58

标签: ionic3

当我的应用程序位于前台时,位置将存储在localStorage中,但是当应用程序在后台运行时,它将不存储

下面是我的离子信息和package.json

离子信息

cli软件包:(C:\ Users \ Pradeep \ AppData \ Roaming \ npm \ node_modules)

@ionic/cli-utils  : 1.9.2
ionic (Ionic CLI) : 3.9.2

全局软件包:

Cordova CLI : 7.1.0

本地软件包:

@ionic/app-scripts : 3.2.3
Cordova Platforms  : android 6.3.0 browser 5.0.4 ios 4.5.4
Ionic Framework    : ionic-angular 3.9.2

系统:

Node : v8.11.3
npm  : 6.0.1

Location tracker.ts代码

  public watch: any;
  public lat: number = 0;
  public lng: number = 0;
  lastUpdatedTime: any = new Date();
  location:any = []

  constructor(
    public zone: NgZone,
    public backgroundGeolocation: BackgroundGeolocation,
    public geolocation: Geolocation,
    public service:HomeService
  ) {

    this.location = localStorage.getItem('locationlist') != null ? JSON.parse(localStorage.getItem('locationlist')) : []
  }

  public startTracking() {

    let config : BackgroundGeolocationConfig = {
      desiredAccuracy: 0,

      debug: true,
      interval: 2000,
      stopOnStillActivity:false,
      stopOnTerminate:false
    };


    this.backgroundGeolocation.configure(config).then((location:BackgroundGeolocationResponse) => {
      alert("sadfgh"+JSON.stringify(location)+"adfsgd")

      alert('BackgroundGeolocation:  ' + location.latitude.toString() + ',' + location.longitude.toString());
      // Update inside of Angular's zone
      this.zone.run(() => {
        this.lat = location.latitude;
        this.lng = location.longitude;
      });
    }, (err) => {
      alert(err+"32142");
    });

    this.backgroundGeolocation.start();

    this.backgroundGeolocation.getLocations().then(data=>{
      alert("zcxv"+JSON.stringify(data))
    })

    // Background tracking
    let options = {
      frequency: 3000,
      enableHighAccuracy: true
    };



    this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.coords != undefined).subscribe((position: Geoposition) => {
      alert(JSON.stringify(position));

      this.zone.run(() => {
        this.lat = position.coords.latitude;
        this.lng = position.coords.longitude;

        var date =  new Date()
        if(date.getTime() - this.lastUpdatedTime > 10000){
          this.location.push({
            coordLat:this.lat,
            coordLong:this.lng,
            Time:date.getFullYear().toString() + "-" + (date.getMonth() + 1).toString() + "-" + date.getDate().toString() + " " + date.getHours().toString() + ":" + date.getMinutes() + ":" + date.getSeconds().toString()
          })
          this.lastUpdatedTime = date.getTime()
          localStorage.setItem("locationlist", JSON.stringify(this.location))
          console.log(date.getTime())
          this.pushLocation(this.location)
          alert("1")
        }

      });


    });

  }

这是下面的代码,请提供帮助。 谢谢

这里我想在后台跟踪或存储地理位置,即当应用最小化时不存储的位置,但是当它在前台工作时可以正常工作。

0 个答案:

没有答案