my.onCompassChange

The my.onCompassChange JSAPI is called by mini program developers to register listeners that monitor compass data updates. The event trigger depends on the prior invocation of my.startCompass, with the callback frequency controlled by the interval parameter passed to my.startCompass.

Parameters

Type

Required

Description

Function listener

Yes

A callback that monitors compass data updates. When executed, it receives a res object containing updated compass data.

res object

Parameter

Type

Required

Description

direction

Number

Yes

The clockwise angle (in degrees) between the device's orientation and true north.

Valid range: [0, 360)

timestamp

Number

Yes

Timestamp when the compass data is recorded.

Sample code

copy
var callback = function (res) {
    console.log(res)
}

// Start compass monitoring
my.startCompass();

// Register the callback for compass data changes
my.onCompassChange(callback);