Taro.onCompassChange(callback)
Listens on the compass data change event at a frequency of 5 times per second. Listening automatically starts after the API is called. You can use Taro.stopCompass
to stop listening.
Type
(callback: Callback) => void
Parameters
Callback
The callback function for the compass data change event.
(result: OnCompassChangeCallbackResult) => void
Property | Type |
---|---|
result | OnCompassChangeCallbackResult |
OnCompassChangeCallbackResult
Property | Type | Description |
---|---|---|
accuracy | string | number | The accuracy |
direction | number | The degree of the direction faced |
accuracy
Accuracy Difference Between iOS and Android The accuracy values are different on iOS and Android.
- iOS: The accuracy is a number-type value indicating the deviation from the magnetic north pole. 0 indicates the device points to magnetic north, 90 east, 180 south, and so on.
- Android: The accuracy is a string-type enumerated value.
Property | Description |
---|---|
high | High accuracy |
medium | Moderate accuracy |
low | Low accuracy |
no-contact | Unreliable. Connection with sensor lost. |
unreliable | Unreliable. Unknown error. |
unknow {value} | An unknown accuracy enumerated value. That is, the value returned by the Android system is not a standard enumerated value of accuracy. |
Sample Code
Taro.onCompassChange(function (res) {
console.log(res.direction)
})
API Support
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
Taro.onCompassChange | ✔️ | ✔️ |