定位
8.1 单次定位与定位控件
const location = new sdk.Geolocation({
enableHighAccuracy:true,timeout:15000,maximumAge:0,
showButton:true,showMarker:true,showCircle:true,
panToLocation:true,buttonPosition:'RB',needAddress:false
});
map.addControl(location);
try {
const fix = await location.getCurrentPosition();
console.log(fix.position,fix.accuracy,fix.heading,fix.speed,fix.timestamp);
} catch(error) { console.error(error.code,error.message); }
| 参数 | 类型 / 单位 | 含义 |
|---|---|---|
| enableHighAccuracy | boolean | 浏览器精度偏好,不保证实际精度 |
| timeout / maximumAge | 毫秒 | 超时 / 最大缓存年龄 |
| showButton / showMarker / showCircle | boolean | 按钮、位置标记、精度圈 |
| panToLocation / zoomToAccuracy | boolean | 成功后回中 / 按精度调整范围 |
| buttonPosition / buttonOffset | 方位 / 像素 | 控件布局 |
| needAddress | boolean | 地址独立异步返回 |
| convert | 仅 false 或省略 | 不能照搬高德 convert:true;坐标模式由 load 决定 |
Geolocation 使用真实浏览器定位权限,不用 IP 或示例坐标伪造定位。SDK 坐标变化不能提升原始传感器精度。
8.2 持续定位与地址
const watchId = location.watchPosition((status,result) => {
if(status === 'complete') console.log(result.position);
else console.warn(result);
});
// 停止:location.clearWatch(watchId);
// 页面释放:location.destroy();
needAddress:true 时监听 address 事件补充地址,不阻塞坐标结果。回调接口与 Promise 错误形态不同;定位公共控件的错误回调为字符串错误码,await 抛 SDKError。浏览器后台、锁屏、省电状态不保证持续回调。