Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GPS position when mobile app is in background (with ionicframework)

I need to realize an app that store user journey (path) when he moves from A to B. Now, I know that ionicframework can use GPS, but what happen when my APP go to background ? How can my app continue to store user position ? Is this possible ? Are there plugins (not $600+ please) that I can use ?

like image 297
ZioBudda Avatar asked Sep 04 '15 14:09

ZioBudda


2 Answers

I just finished writing a new cordova background geolocation plugin for ios and android, its free and simple! try it out:

https://github.com/pmwisdom/cordova-background-geolocation-services

Ionic Usage :

//Wait for cordova / ionic ready to use the plugin
ionic.Platform.ready(function(){
    //Make sure to get at least one GPS coordinate in the foreground before starting background services
    navigator.geolocation.getCurrentPosition();

    //1. Get plugin
    var bgLocationServices =  window.plugins.backgroundLocationServices;

    //2. Configure Plugin
    bgLocationServices.configure({
         desiredAccuracy: 20, 
         distanceFilter: 5, 
         notificationTitle: 'BG Plugin', 
         notificationText: 'Tracking',
         debug: true, 
         interval: 9000, 
         fastestInterval: 5000
    });

    //3. Register a callback for location updates, this is where location objects will be sent in the background
    bgLocationServices.registerForLocationUpdates(function(location) {
         console.log("We got a BG Update" + JSON.stringify(location));
    }, function(err) {
         console.log("Error: Didn't get an update", err);
    });

    //4. Start the Background Tracker. When you enter the background tracking will start, and stop when you enter the foreground.
    bgLocationServices.start();


    ///later, to stop
    bgLocationServices.stop();

});
like image 151
Mirrorcell Avatar answered Nov 01 '22 12:11

Mirrorcell


It's possible with a plugin you're referring as a $600+ premium plugin.

People usually forget that older versions are also available, including last viable Android/iOS version: https://github.com/christocracy/cordova-plugin-background-geolocation/tree/0.3.7

ionic plugin add [email protected]

Currently, other distinct versions do not exist, everything else is just a fork of this original plugin.

like image 42
Gajotres Avatar answered Nov 01 '22 11:11

Gajotres