Compass
startCompass
TIP
The API usage is as follows: wx.startCompass(Object object)
Functional description: Start listening for compass data
Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Sample code:
wx.startCompass()stopCompass
TIP
The API usage is as follows: wx.stopCompass(Object object)
Functional description: Stop listening for compass data
Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Sample code:
wx.stopCompass()onCompassChange
TIP
The API usage is as follows: wx.onCompassChange(function callback)
Functional description: Listen for compass data change events. Frequency: 5 times/second, the interface will automatically start listening after calling, and you can use wx.stopCompass Stops monitoring.
Parameters and descriptions: function callback, The callback function of the compass data change event. The parameters Object res are as follows:
Properties Type Description direction number Facing direction degree accuracy number/string Accuracy Sample code:
wx.onCompassChange(function (res) {
console.log(res.direction)
})Differences in accuracy between iOS and Android: Due to platform differences, the values
iOS: accuracy is a number type value, indicating the deviation from the magnetic north pole. 0 means the device is pointing to the magnetic north, 90 means pointing to the east, 180 means pointing to the south, and so on
Android: accuracy is a string type enumeration value
Value Description high High accuracy medium Medium accuracy low Low accuracy no-contact Untrusted, the sensor lost connection unreliable Untrusted, the reason is unknown unknow ${ value}Unknown accuracy enumeration value, that is, the value representing the accuracy returned by the Android system at this time is not a standard accuracy enumeration value
offCompassChange
TIP
The API usage is as follows: wx.offCompassChange(function listener)
Functional description: Remove listener function for compass data change events
Parameters and descriptions: function listener,onCompassChange passed in the listening function. If this parameter is not passed, all listening functions will be removed
Sample code:
const listener = function (res) { console.log(res) }
wx.onCompassChange(listener)
wx.offCompassChange(listener) // Pass the same function object used for listening.