title: Taro.onCompassChange(callback)

sidebar_label: onCompassChange

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.

Reference

Type

  1. (callback: Callback) => void

Parameters

Callback

The callback function for the compass data change event.

  1. (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

  1. Taro.onCompassChange(function (res) {
  2. console.log(res.direction)
  3. })

API Support

API WeChat Mini-Program H5 React Native
Taro.onCompassChange ✔️ ✔️