'Location tracking in react native even if app is closed?

I am looking for Location tracking in react native even if app is closed?

I have tried react-native-location but how i will track location and get response from gps when even app is closed. I am looking for google timeline functionality, location is even track when app is closed.



Solution 1:[1]

Try this library, but it's not free for Android.

https://github.com/transistorsoft/react-native-background-geolocation

Solution 2:[2]

For such a requirement accomplishment, you should have to create a background task and also need to create a service in android to run up location tracking in the background state and even in the app's kill state.

For this, we can use the below libraries, with the combination of these two libraries the requirement can be implemented.

  1. https://github.com/transistorsoft/react-native-background-fetch
  2. https://github.com/Agontuk/react-native-geolocation-service

React native background fetch is to start the run of a specified task even if the app is in its kill state, and geolocation service to keep track of user location data.

Solution 3:[3]

I had a similar issue where i need to contact an API after a certain time even if app is closed, So I used react-native-background-actions to fetch the API in a loop,you can do the similar

const trackLocation = async () => {
  await new Promise(async () => {
    for (let i = 1; BackgroundService.isRunning(); i++) {
      try {
        // depends on which lib you are using
        await getLocation();
      } catch (error) {
        // console.log(error);
      }
      await sleep(20000);
    }
  });
};

example - https://github.com/Rapsssito/react-native-background-actions#example-code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thisara Sudaraka
Solution 2 Vicky Ahuja
Solution 3 ArnabXD