我每隔60秒跟踪设备位置。这意味着startTracking()
函数需要放在一个区间内。看来,当我这样做时,我没有收到请求,但在我的控制台中看起来gps正在更新,请求就是没有被调用。当我没有在一个区间中放置startTracking()
函数时,它会获得初始GPS位置(显然)并且它没有更新,因为它没有被放置在一个区间中但是正在发送请求。
位置的service.ts
startTracking() {
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.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.gpsProvided = this.diagnostic.isLocationAvailable().then(res => {
return res;
});
this.manifestID = this.events.subscribe('manifestid', maniID => {
return maniID
});
this.backgroundGeolocation.start();
}
stopTracking() {
let url = 'some URL';
let request = {
Latitude: this.lat,
Longitude: this.lng,
Speed: this.speed,
Accuracy: this.accuracy,
Bearing: this.bearing
};
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify(request);
this.backgroundGeolocation.stop();
return this.http.post(url,body,options).subscribe((data) => {
});
}
app.component.ts(无间隔)
constructor(){
this.sendGPSStart()
this.GPSInterval()
}
sendGPSStart(){
this.locationTracker.startTracking();
}
sendGPSStop(){
this.locationTracker.stopTracking();
}
GPSInterval(){
setInterval(() => {
this.sendGPSStop()
})
}, 60000)
}
app.components.ts(有间隔)
constructor(){
this.GPSInterval()
}
sendGPSStart(){
this.locationTracker.startTracking();
}
sendGPSStop(){
this.locationTracker.stopTracking();
}
GPSInterval(){
setInterval(() => {
this.sendGPSStart()
this.sendGPSStop()
})
}, '60000')
}