I’m able to retrieve GPS - lat and long. The issue is, the device is trying to get a GPS reading every second from ionic native geolocation. I’m using both geolocation and background geolocation. The issue is my watch position on the geolocation is running until I unsubscribe (which makes sense). Maybe this is trivial and I’m over thinking but I should be able to unsubscribe right after I subscribe to watch position and retrieve that data correct? Wrong. I’m not able to even read a console.log message inside of the subscription if that’s the case. I’m unsubscribing from watch in the stop tracking but I’m still getting a console message of the GPS data every second until stop tracking is hit, which is every 30 seconds. thanks
map.ts
startTracking() {
console.log('started tracking')
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) => {
console.log('location', location, location.bearing, location.speed, location.longitude);
this.zone.run(() => {
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
});
//foreground tracking
let options: GeolocationOptions = {
timeout: 300000,
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;
this.speed = position.coords.speed;
this.accuracy = position.coords.accuracy
});
console.log(this.lat, this.lng , 'this is the lat lng')
});
this.backgroundGeolocation.start();
}
getGPSData(){
this.gpsunsub = this.service.sendGPS(GPSobjectshere).subscribe(result => {
}
}
}
stopTracking() {
this.backgroundGeolocation.stop();
console.log('stopped tracking')
this.watch.unsubscribe();
this.getGPSData()
this.gpsunsub.unsubscribe()
}
app.component.ts
constructor(){
this.sendGPSStart()
this.interval()
}
sendGPSStart(){
this.locationTracker.startTracking();
}
sendGPSStop(){
this.locationTracker.stopTracking();
}
interval(){
setInterval(() => {
this.sendGPSStart()
this.sendGPSStop()
}, '30000')
}