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.66k stars 426 forks source link

BackgroundGeolocation.onLocation event listener call every time #2144

Closed Rutvik-tbz closed 2 months ago

Rutvik-tbz commented 2 months ago

I want to get the current user location and for that, I use BackgroundGeolocation.getCurrentPosition(), I have also added BackgroundGeolocation.onLocation event listener

whenever I got the user's current location then this event listener call

I want to do like sometimes it calls event listener and sometimes not, it is possible ??

Your Environment

BackgroundGeolocation.ready({
    stationaryRadius: 0,
    desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
    distanceFilter: 0,
    stopTimeout: 5,
    debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
    logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
    stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
    startOnBoot: true, // <-- Auto start tracking when device is powered-up.

    // useSignificantChangesOnly: true,
    batchSync: false,

    stopOnStationary: false,
    allowIdenticalLocations: true,

    locationUpdateInterval: 1000,
    fastestLocationUpdateInterval: 60 * 1000,

    showsBackgroundLocationIndicator: true,
    heartbeatInterval: 60, // 1 min Interval
    preventSuspend: true, // iOS need to work heartbeatInterval
    autoSync: false, // Disable automatic syncing (this doesn't directly prevent permission requests but is a common recommendation)

    // Android & iOS location-services will never turn off
    disableStopDetection: true,
    pausesLocationUpdatesAutomatically: false, // Need for iOS to set location-services will never turn off along with disableStopDetection

})

BackgroundGeolocation.onLocation((location) => {
    console.log(location)
});

// To get the current location of use
export async function getCurrentLocation() {
    try {
        const location = await BackgroundGeolocation.getCurrentPosition({
            samples: 3,
            persist: true,
            desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
            timeout: 30, // seconds
        });

        return {
            lat: location.coords.latitude,
            log: location.coords.longitude,
        };
    } catch (error) {
        console.error('Error getting location:', error);
        return { lat: 0, log: 0 }; // Return 0 values in case of error
    }
}
christocracy commented 2 months ago

Every single location recorded by this plug-in, no matter how, ends up at .onLocation.

you can apply options.extras (see api docs .getCurrentPosition) to your .getCurrentPosition request as a means of adding a flag to discern it in .onLocation.

christocracy commented 2 months ago
const location = await BackgroundGeolocation.getCurrentPosition({
  extras: {
    getCurrentPosition: true // <-- an arbitrary flag of your choice
  }
});