【发布时间】: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