I am tracking the device location every 60 seconds. Which means the start tracking function needs to be placed inside an interval. It seems when I do this, I’m not getting back a request, but in my console it looks like the gps is updating, the request just isn’t being called. When I don’t place the start track function in an interval, it gets the initial GPS location (obviously) and it’s not updating because it’s not placed in an interval but the request is being sent.
location-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 (without interval)
constructor(){
this.sendGPSStart()
this.GPSInterval()
}
sendGPSStart(){
this.locationTracker.startTracking();
}
sendGPSStop(){
this.locationTracker.stopTracking();
}
GPSInterval(){
setInterval(() => {
this.sendGPSStop()
})
}, '60000')
}
app.components.ts (with interval)
constructor(){
this.GPSInterval()
}
sendGPSStart(){
this.locationTracker.startTracking();
}
sendGPSStop(){
this.locationTracker.stopTracking();
}
GPSInterval(){
setInterval(() => {
this.sendGPSStart()
this.sendGPSStop()
})
}, '60000')
}