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

I observer the position of a user like so_someFunc() {navigator.geolocation.watchPosition((position) =&

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745599684a4635326.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信