Help with removing Expo.Location.watchPositionAsync

#1

Hello guys… I’ve been trying to implement Location.watchPositionAsync and it’s working fine… but I just can’t figure out how to remove the subscription, the code is as follows:

_checkIn = async () => {
    if (this.state.checked_in) {
      Alert.alert("YOU ARE ALREADY CHECKED IN!");
    } else {
      this.setState({ checked_in: true, });
      Alert.alert("SUCCESSFULLY CHECKED IN!");
      Expo.Location.watchPositionAsync({
        enableHighAccuracy: true,
        distanceInterval: 10,
      }, NewLocation => {
          let coords = NewLocation.coords;
          console.log("NEW LOCATION COORDS", coords);
     });
    }
  }

_checkOut = async () => {
    if (!this.state.checked_in) {
      Alert.alert("YOU ARE ALREADY CHECKED OUT!");
    } else {
      Expo.Location.watchPositionAsync().remove();
      this.setState({ checked_in: false, });
      Alert.alert("SUCCESSFULLY CHECKED OUT!");
    }
  }

Note: I tried different variations to remove the subscription… but none seems to work. What am I doing wrong?

#2

Keep the return value of the original call to watchPositionAsync and store it somewhere and then call .remove() on that

Above, you’re setting up a new watch and immediately removing it but leaving the original one around.

1 Like
#3

Thanks a lot @ccheever it seems I have forgotten to await the return value.

1 Like
#4

Hi, Ahmad! Sorry if my message is offtopic in this topic, but can you show me please full work example’s code where you are using watchPositionAsync and it is working fine. Thank you for early and sorry for bad english :slight_smile:

2 Likes