rnn
rnn

Reputation: 2563

How to get my geolocation logs even my app close

I want to get my location coordinate even app close. How do I do this ?

I use code which is below for get my location coordinate every 1 sec but it gives me always same coordinate. I dont know why it is not update.

even this is work, I cant get coordinate when app close. any idea about this ?

I did this :

 componentDidMount() {
        this.interval = setInterval(() => {

navigator.geolocation.getCurrentPosition((position) => {
            var lat = parseFloat(position.coords.latitude);
            var lng = parseFloat(position.coords.longitude);

              console.log("lat :",lat);
              console.log("lng :",lng);

              this.setState({position1: lat, position2: lng});

          },
          error => Alert.alert(error.message),
                { enableHighAccuracy: true, timeout: 30000, maximumAge: 1000 } 
          );

        }, 1000);
      }

      componentWillUnmount() {
        clearInterval(this.interval);
      }

Upvotes: 2

Views: 165

Answers (1)

Subhendu Kundu
Subhendu Kundu

Reputation: 3856

If the app is in the background or brought to the foreground, you might wanna take the advantage of AppState api and AppState.addEventListener('change', this._handleAppStateChange); see for the location change. If the app is closed, I am not quite sure if react native has an api watching location directly if the app is closed. However you can use react-native-queue to do this. You might wanna take a look at https://github.com/billmalarky/react-native-queue#os-background-task-full-example.

Upvotes: 1

Related Questions