最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to call stopObserving() from navigator.geolocation properly? - Stack Overflow

matteradmin3PV0评论

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?

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?

Share Improve this question asked Sep 25, 2017 at 12:28 four-eyesfour-eyes 12.6k37 gold badges130 silver badges255 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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();
Post a comment

comment list (0)

  1. No comments so far