【问题标题】:Background Geolocation For Ionic2Ionic2 的背景地理位置
【发布时间】:2017-06-08 21:02:55
【问题描述】:

我正在尝试使用背景地理位置,我找到了这个链接: [链接]https://www.joshmorony.com/adding-background-geolocation-t…/。 “地理位置未知”类型脚本错误。

ionic 版本:2.2.1,node 版本:6.10.0,cordova 版本:6.5.0。对 ionic2 geoloca 的任何建议

location-tracker :


import { Injectable, NgZone } from '@angular/core';
import { Geolocation,Geoposition, BackgroundGeolocation } from 'ionic-native';
import 'rxjs/add/operator/filter';
 
@Injectable()
export class LocationTracker {
 
  public watch: any;    
  public lat: number = 0;
  public lng: number = 0;
 
  constructor(public zone: NgZone) {
 
  }
 
  startTracking() {
 // Background Tracking
 
  let config = {
    desiredAccuracy: 0,
    stationaryRadius: 20,
    distanceFilter: 10, 
    debug: true,
    interval: 2000 
  };
 
  BackgroundGeolocation.configure((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);
 
  }, config);
 
  // Turn ON the background-geolocation system.
  BackgroundGeolocation.start();
 
 
  // Foreground Tracking
 
  let options = {
    frequency: 3000, 
    enableHighAccuracy: true
  };
 
  this.watch = 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');
 
  BackgroundGeolocation.finish();
  this.watch.unsubscribe();
 
  }
 
}

【问题讨论】:

  • 请向我们展示实际代码..

标签: background geolocation ionic2


【解决方案1】:

假设你已经运行了以下命令;

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

尝试按如下方式导入插件:

import { BackgroundGeolocation } from '@ionic-native/background-geolocation';
import { Geolocation, Geoposition } from '@ionic-native/geolocation';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2016-12-23
    • 2021-12-29
    • 2015-01-11
    • 2021-04-15
    相关资源
    最近更新 更多