0

I'm currently developing a mobile app using AngularJS / Ionic 3.

I need to track at all time, if my users are within a radius of 100m of a certain geolocation (let's call it "Home Location". I need to know at all time which user is at his home location, and who is not (even when the app is running in background or is closed/ terminated).

I thought to realize this, using the Ionic Native Background-Geolocation plugin and the cordova-plugin-background-geolocation. My plan was, to check the users geolocation every 5 minutes and compare it to the home.

Case 1) If the distance between the two locations is < 100m I know the user is "at home". I would then update the user node in my database (Firebase) to mark the user as isAtHome: true and add a current timestamp.

Case 2) If the user is not within 100m of his home location I would mark him as isAtHome: false and add a current timestamp.

I need to update my database even in case the user didn't move, because I need to know that I received a current signal. Otherwise I don't know if he didn't move or turned off his smartphone, for example.

If I want to know, who of my users is at his home location, I would check the isAtHome-attributes and if they are set to true, I would check the timestamp to be sure that I have up-to-date data and the data was written within the last 15 minutes.

I tried many different things, but I'm not sure how to realize this with the Cordova Background Geolocation Plugin and if it's even possible.

My question is:

Is it possible to check the users geolocation every 5 minutes (even in background / terminated state) and call a function that updates my firebase DB accordingly? As described, I need to receive the location even if the user didn't move within the last 5 minutes.

If it isn't possible: Does anybody have an idea, of how to approach the requirements on another way?

Thank you very much for your help!

Jane Dawson
  • 693
  • 2
  • 7
  • 19

1 Answers1

2

As described in the cordova-plugin-background-geolocation says, you can configure an interval in milliseconds for the plugin to get the location, see this example from the plugin github page:

backgroundGeolocation.configure(callbackFn, failureFn, {
    desiredAccuracy: 10,
    stationaryRadius: 20,
    distanceFilter: 30,
    url: 'http://192.168.81.15:3000/locations',
    httpHeaders: { 'X-FOO': 'bar' },
    maxLocations: 1000,
    // Android only section
    locationProvider: backgroundGeolocation.provider.ANDROID_ACTIVITY_PROVIDER,
    interval: 60000, // <-- here
    fastestInterval: 5000,
    activitiesInterval: 10000,
    notificationTitle: 'Background tracking',
    notificationText: 'enabled',
    notificationIconColor: '#FEDD1E',
    notificationIconLarge: 'mappointer_large',
    notificationIconSmall: 'mappointer_small'
});

The case is this interval property works only for Android. I would sugest a workaround like this:

public getLocation: boolean = true; // CREATE A BOOLEAN TO KNOW IF YOU CAN GET LOCATION

backgroundGeolocation.start( // i've never used this plugin, so i'm assuming this is what starts de background watcher
  () => {
    if(getLocation == true){
      this.getLocation = false; // SET TO FALSE SO IT DON'T DO YOUR FUNCTION THE NEXT TIME IT GETS THE LOCATION

      // DO YOUR CODE TO SAVE AND CHECK YOUR DATA
      // IT'S BETTER IF YOU USE A PROMISE, ASYNC/AWAIT FOR THE SAVING CODE
      yourSavingCodeFunction().then(() => {
        setTimeout(()=>{
          // AFTER 5 MINUTES, SET THE GETLOCATION TO TRUE SO IT CAN DO YOUR FUNCTION AGAIN
          this.getLocation = true;
        }, 500000);
      });
    }
  },
  (error) => {
    // Tracking has not started because of error
    // you should adjust your app UI for example change switch element to indicate
    // that service is not running
  }
);

If you haven't thought about your cases 1 and 2 we can work on this too. Hope this helps or can give you some ideas :D

Gabriel Barreto
  • 6,411
  • 2
  • 24
  • 47
  • Thanks, Gabriel. I thought about a similar solution. However, the problem is: If the device doesn't change it's position for 20 minutes, then 'yourSavingCodeFunction()' is not executed within this timeframe. So my db is not updated and I don't have current location data. How would I then know the difference between a not-moving user and a switched off mobile phone for example? How would I be able to say for sure if the user is at his home location or not? – Jane Dawson Jul 04 '17 at 06:39
  • Why can't you update your location every 5 minutes even if the user hasn't moved? Is there a point why you just need to update only if it moves and if 20 minutes without moving? Updating it even if the user hasn't moved seems better since you don't need to check and do calculations about the current location, so it's one step less. – Gabriel Barreto Jul 04 '17 at 12:19
  • Actually, the plugin doesn't allow to update the location every x minutes for iOS, as the owner states here: https://forum.ionicframework.com/t/get-location-background-and-foreground-every-5-minutes-for-private-app/31101/9 – Jane Dawson Jul 04 '17 at 18:04