-
Notifications
You must be signed in to change notification settings - Fork 8k
/
index.md
74 lines (53 loc) · 1.83 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
title: Geolocation.watchPosition()
slug: Web/API/Geolocation/watchPosition
---
{{ APIref("Geolocation API") }}
**`Geolocation.watchPosition()`** 用于注册监听器,在设备的地理位置发生改变的时候自动被调用。也可以选择特定的错误处理函数。
该方法会返回一个 ID,如要取消监听可以通过 {{domxref("Geolocation.clearWatch()")}} 传入该 ID 实现取消的目的。
## 语法
```js-nolint
watchPosition(success)
watchPosition(success, error)
watchPosition(success, error, options)
```
### 参数
- _success_
- : 成功时候的回调函数,同时传入一个 {{domxref("Position")}} 对象当作参数。
- _error_ {{optional_inline}}
- : 失败时候的回调函数,可选,会传入一个 {{domxref("PositionError")}} 对象当作参数。
- _options_ {{optional_inline}}
- : 一个可选的 {{domxref("PositionOptions")}} 对象。
## 示例
```js
var id, target, options;
function success(pos) {
var crd = pos.coords;
if (target.latitude === crd.latitude && target.longitude === crd.longitude) {
console.log("Congratulations, you reached the target");
navigator.geolocation.clearWatch(id);
}
}
function error(err) {
console.warn("ERROR(" + err.code + "): " + err.message);
}
target = {
latitude: 0,
longitude: 0,
};
options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0,
};
id = navigator.geolocation.watchPosition(success, error, options);
```
## 规范
{{Specifications}}
## 浏览器兼容性
{{Compat}}
## 参见
- [使用地理位置定位](/zh-CN/docs/WebAPI/Using_geolocation)
- 该方法属于 {{domxref("Geolocation")}},可以通过 {{domxref("NavigatorGeolocation.geolocation")}} 访问。
- 取消监听的方法: {{domxref("Geolocation.clearWatch()")}}
- 另一个类似的方法: {{domxref("Geolocation.getCurrentPosition()")}}