Unable to remove Expo Location.watchPositionAsync()

Viewed 1349

I wanted to be able to track user locations for some features in my application, but I want users to be able to turn off location if they want. I've been using expo-location to find the users location, request permission and varies other things. But when I use watchPositionAsync, I can't remove it when the user turns it off the feature. I read the docs and it said that I could use remove() but I get the following error when I try to do that:

Unhandled promise rejection: TypeError: client.location.remove is not a function. (In 'client.location.remove()', 'client.location.remove' is undefined)

For reference client is just a global object which stores some settings for the user (It's so I don't have to keep fetching it over and over again).

client.js

let client = {
   user: {},
   location: null
};

tracking.js

import * as Location from 'expo-location';

export async function startTracking(client){
    console.log('Starting tracking')
    if(!client.location){
        client.location = Location.watchPositionAsync({
            accuracy: Location.Accuracy.Highest,
            distanceInterval: 1,
            timeInterval: 5000,
        }, (loc) => {
            console.log(loc)
        });
    }
}


export async function stopTracking(client){
    console.log('Remove tracking')
    await client.location.remove();
}

More information:

  • Expo version - 40.0.1
  • Android version - Oxygen OS 10.5.11
  • React native version - 0.63.4
  • expo-location version - 10.0.0
  • Client.js has a better structure than the one above...
1 Answers

You are not waiting the returned object by the function watchPositionAsync.

Returns a promise resolving to a subscription object, which has one field

I take this source.

Try this code.

import * as Location from 'expo-location';

export async function startTracking(client){
    console.log('Starting tracking')
    if(!client.location){
        client.location = await Location.watchPositionAsync({
            accuracy: Location.Accuracy.Highest,
            distanceInterval: 1,
            timeInterval: 5000,
        }, (loc) => {
            console.log(loc)
        });
    }
}


export async function stopTracking(client){
    console.log('Remove tracking')
    await client.location.remove();
}

Related