Skip to main content
Version: 3.x

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.

Reference

Type

(callback: Callback) => void

Parameters

Callback

The callback function for the compass data change event.

(result: OnCompassChangeCallbackResult) => void
PropertyType
resultOnCompassChangeCallbackResult

OnCompassChangeCallbackResult

PropertyTypeDescription
accuracystring | numberThe accuracy
directionnumberThe 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.
PropertyDescription
highHigh accuracy
mediumModerate accuracy
lowLow accuracy
no-contactUnreliable. Connection with sensor lost.
unreliableUnreliable. 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

APIWeChat Mini-ProgramH5React Native
Taro.onCompassChange✔️✔️