概述
Geolocation API 提供设备位置获取与追踪功能,需要用户授权与安全上下文。需权衡精度、功耗与隐私,并提供回退方案。
示例
```js
navigator.geolocation.getCurrentPosition(pos => {
const { latitude, longitude, accuracy } = pos.coords
}, err => {}, { enableHighAccuracy: true, timeout: 5000, maximumAge: 10000 })
const id = navigator.geolocation.watchPosition(pos => {
const { latitude, longitude } = pos.coords
})
navigator.geolocation.clearWatch(id)
```
工程建议
- 精度与功耗:仅在必要时启用高精度;设置合理 `timeout/maximumAge`。
- 隐私与合规:透明告知用途与范围,最小化采集与存储,提供关闭入口。
- 回退:不支持时提供手动输入地址或 IP 粗定位。
参考与验证
- MDN Geolocation 文档:https://developer.mozilla.org/docs/Web/API/Geolocation_API
- W3C 规范:https://www.w3.org/TR/geolocation/
- web.dev 位置与权限指南:https://web.dev/articles/location

发表评论 取消回复