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.
Supported types of mini programs: DSL, H5, H5+
Parameters
Type | Required | Description |
Function listener | Yes | A callback that monitors compass data updates. When executed, it receives a |
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
DSL
copy
var callback = function (res) {
console.log(res)
}
// Start compass monitoring
my.startCompass();
// Register the callback for compass data changes
my.onCompassChange(callback);H5, H5+
copy
AlipayJSBridge.call('watchShake',{monitorCompass:true}, function(data) {
alert('Result for ' + JSON.stringify(data));
});
function compassChangehandler(res) {
alert('Result for ' + JSON.stringify(res));
}
window.document.addEventListener('compassChange', compassChangehandler)