# uni.onCompassChange(CALLBACK)

Monitor compass data, frequency: 5 times per second, it will automatically start monitoring after the interface is called, you can use uni.offCompassChange to cancel the monitoring.

CALLBACK return parameter

Parameter Type Description
direction Number Facing direction in degrees

Tips

  • The H5 terminal needs to be deployed on the https service to obtain compass information, and the local preview (localhost) can still use the http protocol.

example

const callback = function (res) {
	console.log(res.direction);
}
uni.onCompassChange(callback);

# uni.offCompassChange(CALLBACK)

Cancel listening to compass data.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
HBuilderX 2.9.6+ HBuilderX 2.9.6+ Basic library 2.9.3+ x x x x x

example

const callback = function (res) {
	console.log(res.direction);
}
uni.onCompassChange(callback);
// Just pass in the same function as onCompassChange
uni.offCompassChange(callback);

Tips

  • CALLBACK is the CALLBACK passed in when calling uni.onCompassChange

# uni.startCompass(OBJECT)

Start listening to compass data.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
x x

OBJECT parameter description

Parameter Name Type Required Description
success Function No Callback for successful interface call
fail Function No Callback function for interface call failure
complete Function No The callback function for the end of the interface call (it will be executed when the call succeeds or fails)

example

uni.startCompass();

# uni.stopCompass(OBJECT)

Stop listening to compass data.

OBJECT parameter description

Parameter Name Type Required Description
success Function No Callback for successful interface call
fail Function No Callback function for interface call failure
complete Function No The callback function for the end of the interface call (it will be executed when the call succeeds or fails)

example

uni.stopCompass();