Reputation: 3431
I'm working with Ionic and Cordova plugins. I had an app which worked with Cordova Background-mode and Geolocation plugins:
this.backgroundMode.enable();
this.backgroundMode.on("activate").subscribe(() => {
this.backgroundMode.disableWebViewOptimizations(); // For GPS tracking at background
});
let watch = this.geolocation.watchPosition({enableHighAccuracy: true});
watch.subscribe((resp) => {
// Use resp.latitude and resp.longitude
});
Until I realised that exists BackgroundGeolocation plugin which, in many users' opinion is much better and faster than using both plugins.
My problem is that I don't know how to replicate my old code with this plugin, I tried with:
const config: BackgroundGeolocationConfig = {
desiredAccuracy: 0, // Best accuracy possible
stationaryRadius: 0,
distanceFilter: 0
};
this.backgroundGeolocation.configure(config).subscribe((location: BackgroundGeolocationResponse) => {
// Use location.latitude and location.longitude,
});
But this solution is not as "Real time" as the first one.
Thanks in advance and sorry about my English
Upvotes: 0
Views: 854
Reputation: 177
After days of searching and trying with it, I found that we should use Geolocation after BackgroundGeolocation. It works exactly as expected.
const config: BackgroundGeolocationConfig = {
desiredAccuracy: 1,
stationaryRadius: 20,
distanceFilter: 10,
debug: true, // enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false, // enable this to clear background location settings when the app terminates
fastestInterval: 4000,
interval: 10000
};
this.backgroundGeolocation.configure(config)
.then(() => {
this.backgroundGeolocation.on(BackgroundGeolocationEvents.location).subscribe((location: BackgroundGeolocationResponse) => {
console.log(location);
this.http.post('http://riyadkhalifeh.com/insert.php', { log: location.latitude, lat: location.longitude }, {})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
})
.catch(error => {
console.log(error);
console.log(error); // error message as string
console.log(error);
});
});
});
// start recording location
this.backgroundGeolocation.start();
//////////////
this.geolocation.getCurrentPosition().then((resp) => {
this.http.post(url, { lat: resp.coords.latitude, long: resp.coords.longitude }, {})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
})
.catch(error => {
console.log(error);
console.log(error); // error message as string
console.log(error);
});
}).catch((error) => {
console.log('Error getting location', error);
});
let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
this.http.post(url, { lat: data.coords.latitude, long: data.coords.longitude }, {})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
})
.catch(error => {
console.log(error);
console.log(error); // error message as string
console.log(error);
});
});
Good luck for everybody
Upvotes: 1
Reputation: 32
That should work, and if it is in real time but .configure
is an observable and only listen when the coordinates change.
Upvotes: 0