【问题标题】:background geolocation information to send to server ionic 2+后台地理位置信息发送到服务器 ionic 2+
【发布时间】:2018-02-26 13:11:09
【问题描述】:

我想从后台地理定位插件向我的服务器发送不同的配置设置。但是,我不确定如何让它发挥作用。我已经准备好大部分位置服务器。只是不确定如何发送该数据。我想将开始跟踪配置放在发布请求中。但不确定如何检索该信息以及我下面的代码是否正确。 谢谢!

location.ts

import { Injectable, NgZone } from '@angular/core';
import { BackgroundGeolocation, BackgroundGeolocationConfig } from '@ionic-native/background-geolocation';
import { Geolocation, Geoposition } from '@ionic-native/geolocation';
import 'rxjs/add/operator/filter';

@Injectable()
export class LocationTracker {

  public watch: any;    
  public lat: number = 0;
  public lng: number = 0;
  public bearing: number = 0;
  public speed: number = 0;

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

  }

  startTracking() {

    let config = {
    desiredAccuracy: 0,
    stationaryRadius: 20,
    distanceFilter: 10, 
    debug: true,
    interval: 2000,
    bearing: 10,
    speed: 0,

  };

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

    console.log('BackgroundGeolocation:  ' + location.latitude + ',' + location.longitude);

    // Run update inside of Angular's zone
    this.zone.run(() => {
      this.lat = location.latitude;
      this.lng = location.longitude;
    });

  }, (err) => {

    console.log(err);

  });

  // Turn ON the background-geolocation system.
  this.backgroundGeolocation.start();


  // Foreground Tracking

let options = {
  frequency: 60000, 
  enableHighAccuracy: true
};

this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {

  console.log(position);

  // Run update inside of Angular's zone
  this.zone.run(() => {
    this.lat = position.coords.latitude;
    this.lng = position.coords.longitude;
  });

});

  }

  stopTracking() {
        console.log('stopTracking');
        this.backgroundGeolocation.finish();
        this.watch.unsubscribe();
  }

}

【问题讨论】:

  • 你解决了吗?

标签: cordova ionic-framework ionic2 ionic3


【解决方案1】:

您的代码适用于 Ionic 4。不确定它是否适用于 ionic 2

这就是我所做的。

https://ionicframework.com/docs/native/background-geolocation 的说明非常好。只少了一小部分。

丢失的代码是我认为重要的,以便继续发送经纬度的位置。我突出显示缺少的代码。

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

constructor(private backgroundGeolocation: BackgroundGeolocation) { }

...
// Wrap this in a function to activate

const config: BackgroundGeolocationConfig = {
            desiredAccuracy: 10,
            stationaryRadius: 2, //I reduce this from 20 to 2
            distanceFilter: 3, //I reduce this from 30 to 3
            interval: 2000, //This is missing from example
            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)
  .then(() => {

    this.backgroundGeolocation.on(BackgroundGeolocationEvents.location).subscribe((location: BackgroundGeolocationResponse) => {

  console.log(location);

  //DO YOUR HTTP POST HERE TO SEND LOCATION LAT AND LONG TO SERVER

      // 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 operations are 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.
// Wrap this inside a call off function to stop backgroundGeolocation
this.backgroundGeolocation.stop();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多