starCompass
该 API 使用方法为 wx.startCompass(Object object)
功能说明:开始监听罗盘数据。
参数及说明:Object object。
属性 | 类型 | 默认值 | 必填 | 说明 |
success | function | - | 否 | 接口调用成功的回 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.startCompass()
stopCompass
该 API 使用方法为 wx.stopCompass(Object object)
功能说明:停止监听罗盘数据,使用方法为。
参数及说明:Object object。
属性 | 类型 | 默认值 | 必填 | 说明 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码
wx.stopCompass()
onCompassChange
该 API 使用方法为 wx.onCompassChange(function callback)
功能说明:监听罗盘数据变化事件。频率:5次/秒,接口调用后会自动开始监听,可使用 wx.stopCompass 停止监听。
参数及说明:function callback。
罗盘数据变化事件的回调函数:Object res
属性 | 类型 | 说明 |
direction | number | 面对的方向度数 |
accuracy | number/string | 精度 |
示例代码
wx.onCompassChange(function (res) {console.log(res.direction)})
accuracy 在 iOS/Android 的差异:由于平台差异,accuracy 在 iOS/Android 的值不同。
iOS:accuracy 是一个 number 类型的值,表示相对于磁北极的偏差。0表示设备指向磁北,90表示指向东,180表示指向南,依此类推。
Android:accuracy 是一个 string 类型的枚举值。
值 | 说明 |
high | 高精度 |
medium | 中等精度 |
low | 低精度 |
no-contact | 不可信,传感器失去连接 |
unreliable | 不可信,原因未知 |
unknow ${value} | 未知的精度枚举值,即该 Android 系统此时返回的表示精度的 value 不是一个标准的精度枚举值 |
offCompassChange
该 API 使用方法为 wx.offCompassChange(function listener)
功能说明:移除罗盘数据变化事件的监听函数。
参数及说明:function listener,onCompassChange 传入的监听函数。不传此参数则移除所有监听函数。
示例代码:
const listener = function (res) { console.log(res) }wx.onCompassChange(listener)wx.offCompassChange(listener) // 需传入与监听时同一个的函数对象