Skip to content

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。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
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。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
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:

    PropertiesTypeDescription
    directionnumberFacing direction degree
    accuracynumber/stringAccuracy
  • Sample code:

js
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

    ValueDescription
    highHigh accuracy
    mediumMedium accuracy
    lowLow accuracy
    no-contactUntrusted, the sensor lost connection
    unreliableUntrusted, 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:

js
const listener = function (res) { console.log(res) }

wx.onCompassChange(listener)
wx.offCompassChange(listener) // Pass the same function object used for listening.