---
title: Geolocation API:权限、精度与隐私策略
keywords:
- Geolocation
- getCurrentPosition
- watchPosition
- enableHighAccuracy
- 权限与隐私
description: 说明地理位置的读取与监听、精度/时效参数与功耗权衡、隐私与合规策略,并提供示例与回退。
categories:
- 文章资讯
- 技术教程
---
概述
Geolocation API 提供设备位置获取与追踪功能,需要用户授权与安全上下文。需权衡精度、功耗与隐私,并提供回退方案。
示例
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

发表评论 取消回复