以Promise风格调用: 支持
获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。开启高精度定位,接口耗时会增加,可指定 highAccuracyExpireTime 作为超时时间。地图相关使用的坐标格式应为 gcj02。高频率调用会导致耗电,如有需要可使用持续定位接口 xhs.onLocationChange
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
type | string | wgs84 | 否 | wgs84 返回 gps 坐标,gcj02 返回可用于 xhs.openLocation 的坐标。iOS支持 |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
---|---|---|
latitude | number | 纬度,范围为 -90~90,负数表示南纬 |
longitude | number | 经度,范围为 -180~180,负数表示西经 |
speed | number | 速度,单位 m/s |
accuracy | number | 位置的精确度 |
altitude | number | 高度,单位 m |
verticalAccuracy | number | 垂直精度,单位 m(Android 无法获取,返回 0) |
horizontalAccuracy | number | 水平精度,单位 m(iOS 无法获取,返回 0) |
errMsg | string |
// 示例代码:以 Promise 风格调用 getLocation function getLocation(options = {}) { return new Promise((resolve, reject) => { xhs.getLocation({ ...options, success: (res) => resolve(res), fail: (err) => reject(err), }); }); } // 调用示例 getLocation({ type: 'gcj02', // 返回可用于 xhs.openLocation 的坐标 }) .then((res) => { console.log('地理位置获取成功:'); console.log('纬度:', res.latitude); console.log('经度:', res.longitude); console.log('速度:', res.speed, 'm/s'); console.log('位置精度:', res.accuracy, 'm'); console.log('高度:', res.altitude, 'm'); console.log('垂直精度:', res.verticalAccuracy, 'm'); console.log('水平精度:', res.horizontalAccuracy, 'm'); }) .catch((err) => { console.error('地理位置获取失败:', err); });