Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call stopObserving() from navigator.geolocation properly?

I observer the position of a user like so

_someFunc() {
    navigator.geolocation.watchPosition((position) => {
        ...
    )};
}

_someFunc() is called in an _onPress(). In another onPress() I want to stop the watchPosition. Doing that, I use the stopObserving like (badly?) documented here. On another onPress() I simply call

_anotherSomeFunc() {
    navigator.geolocation.stopObserving();
}

However, that gives me the warning:

Called stopObserving without existing subscriptions.

I have no idea what to make of it. What kind of subscriptions?

like image 425
Stophface Avatar asked Sep 25 '17 12:09

Stophface


1 Answers

You should save the watchId returned by navigator.geolocation.watchPosition , and then call clearWatch:

watchId = navigator.geolocation.watchPosition((position) => {
        ...
    }
);

...

navigator.geolocation.clearWatch(watchId);
navigator.geolocation.stopObserving();
like image 84
yoavr Avatar answered Nov 14 '22 22:11

yoavr