transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.63k stars 425 forks source link

[Question] Strategy to Ignore `in_vehicle` Activities for Walking App #2161

Closed chrisbiscuit closed 2 days ago

chrisbiscuit commented 1 week ago

Hi,

I’m developing a walking app where I would like to ignore location tracking when users are in vehicles or on bicycles. The goal is to only record locations when the user starts walking or running. I've been exploring the onActivityChange event and different configurations to achieve this.

What I'm Trying to Achieve:

Approach I've Considered:

  1. Using onActivityChange to Start and Stop Tracking: I was thinking of listening to the onActivityChange event and starting/stopping location tracking based on the activity type. Here’s what I’ve considered so far:

    BackgroundGeolocation.onActivityChange((activity) => {
     console.log("[onActivityChange] - ", activity);
    
     if (activity.type === 'on_foot') {
       // Start tracking when walking
       BackgroundGeolocation.start();
     } else if (activity.type === 'in_vehicle' || activity.type === 'on_bicycle') {
       // Stop tracking when in a vehicle or on a bike
       BackgroundGeolocation.stop();
     }
    });

    However, I'm concerned that stopping BackgroundGeolocation when the user is in a vehicle may cause the system to stop listening for future activity changes, preventing tracking from restarting when the user starts walking again.

  2. Pausing Location Updates Instead of Stopping: Another approach I considered is using changePace(false) to pause tracking rather than completely stopping it, but only when the user is in a vehicle. Then, I'd resume tracking with changePace(true) once they are on foot again.

    BackgroundGeolocation.onActivityChange((activity) => {
     if (activity.type === 'on_foot') {
       // Resume aggressive tracking
       BackgroundGeolocation.changePace(true);
     } else if (activity.type === 'in_vehicle' || activity.type === 'on_bicycle') {
       // Pause tracking when in a vehicle
       BackgroundGeolocation.changePace(false);
     }
    });

    My concern here is that if changePace(false) is set, it might not aggressively restart location tracking until a significant time has passed or the activity changes to something like walking.

My Questions:

  1. What is the best strategy to completely ignore location updates when a user is in a vehicle or on a bike, but resume tracking immediately when they start walking?
  2. Is there a way to ensure the system continues to monitor activity changes even when tracking is paused or stopped for certain activities (like in_vehicle)?
  3. Are there configuration settings I can leverage to fine-tune this behavior, or is the approach of manually handling onActivityChange recommended?

I’d appreciate any guidance or alternative suggestions on how to efficiently implement this.

Thanks!

christocracy commented 1 week ago

Android has Config.triggerActivities but I don’t think I implemented for iOS. I’ll investigate.

chrisbiscuit commented 1 week ago

Thanks for the response!

We had already looked at Config.triggerActivities for Android, but we definitely need something similar for iOS as well.

Here’s a quick example of how we're using it on Android:

BackgroundGeolocation.ready({
  triggerActivities: 'on_foot, running', 
});

We'd really appreciate any updates on whether this can be extended to iOS or if there's another iOS-specific approach we could use.

Thanks again.

christocracy commented 2 days ago

Please try installing version 4.17.3-rc.1 with iOS implementation for Config.triggerActivities.

chrisbiscuit commented 2 days ago

Thanks so much for adding the iOS implementation for Config.triggerActivities. I really appreciate the quick turnaround on this.

I meant to post my original message on the capacitor-background-geolocation repo, not the react-native-background-geolocation one. Apologies for the mix-up!

Would you like me to raise a ticket on the correct GitHub repo (capacitor-background-geolocation), or is it fine to leave it here? Just let me know what works best for you.

christocracy commented 2 days ago

It’s crucial that you seek support at the appropriate repo for the product you’re using.

I manage this plug-in for 4 different development frameworks, not just the one you happen to be using.