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.54k stars 424 forks source link

Getting duplicate callbacks from BackgroundGeolocation.onLocation while network switch and app state change. #1984

Closed Sarveshwins closed 1 month ago

Sarveshwins commented 1 month ago

I'm getting multiple entries while I'm changing the network state or changing app state(Kill state to Foreground state) and this data is coming twice with same data values.

Now if I'm trying to filter out data then sometimes behavior of this library is different, Sometimes it provides callback multiple times and sometimes it's not giving callback on location change.

christocracy commented 1 month ago

Every time the state of location providers changes (eg disable wifi, disable location), the .onProviderChange event fires. A location is recorded so you can know where the device was when the state changed. This is noted in location.event == ‘providerchange’.

see api docs Config.disableProviderChangeRecord.

Sarveshwins commented 1 month ago

@christocracy I'm facing issue of duplicate data and in nano seconds I'm getting same data twice from BackgroundGeolocation.onLocation. I understand that BackgroundGeolocation.onProviderChange is providing event of providerChange but here the issue is with multiple data from BackgroundGeolocation.onLocation.

We want to track location of the person I'm also attaching Excel sheet, Please check and let me know what can I do to avoid this twice data. https://docs.google.com/spreadsheets/d/1jKMtPPbsYeuw1Jvaga5ZUK9A4_2f9Uh-KT0pcgYYZ2c/edit#gid=0

christocracy commented 1 month ago

Well, you ignored the issue template which requested a bunch of information, including “iOS or Android?”, plug-in version?, Config?, etc.

Sarveshwins commented 1 month ago

I'm using react-native-background-geolocation version 4.15.0 and react-native version is 0.64.0 and react version is 16.13.1 and config is below

const state = await BackgroundGeolocation.ready({
        // url: 'https://655469d763cafc694fe66d97.mockapi.io/getLocation',
        url: API_URL + API_ENDPOINTS.SAVELOCATION,
        logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
        desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
        distanceFilter: 1,
        stopTimeout: 5,
        batchSync: false,
        autoSync: false,
        maxDaysToPersist: 1,
        stopOnTerminate: false,
        startOnBoot: true,
        enableHeadless: true,
        maxRecordsToPersist: 2,
        // maxBatchSize: 10000,
        // autoSyncThreshold: 5,
 locationTemplate:
          '{"Latitude":<%= latitude %>,"Longitude":<%= longitude %>,"TimeStamp": "<%= timestamp %>"}',
        locationAuthorizationRequest: 'Always',
        backgroundPermissionRationale: {
          title:
            "Allow {applicationName} to access this device's location even when closed or not in use.",
          message:
            'This app collects location data to enable recording your trips to work and calculate distance-travelled.',
          positiveAction: 'Change to "{backgroundPermissionOptionLabel}"',
          negativeAction: 'Cancel',
        },
        headers: {
          authorization: 'Bearer ' + getToken.data.Token,
          source: source,
        },
christocracy commented 1 month ago

distanceFilter: 1,

You have an aggressive distanceFilter.

Also see Config.allowIdenticalLocations

If you don't like some particular location at your server, you should filter it out there at your server.

If you don't want providerchange records to be recorded, disable it with Config.disableProviderChangeRecord.

Sarveshwins commented 1 month ago

@christocracy I tried changing distanceFilter from 1 to 10 but still facing same issue. Is there any other property available to handle this duplicate data?

christocracy commented 1 month ago

Is there any other property available to handle this duplicate data?

I told you above. If that doesn’t work, then filter your data as desired at the server.

christocracy commented 1 month ago

Also, locations are not duplicate unless Location.uuid is identical (you’re not providing that to your locationTemplate.

you might also want to be aware of Location.age.

it’s not unusual to have a “duplicate” lat/lng.