All Products
Search
Document Center

Mobile Platform as a Service:Compass

Last Updated:Feb 04, 2021

my.onCompassChange(function callback)

Note:
  • This interface is supported since basic library version 1.9.0. Compatibility processing is required for earlier versions. See Mini Program base library to learn more.
  • This interface is only supported in mPaaS 10.1.60 and later versions.

Use this API to listen to the compass data. After the interface is called, the listening is automatically started. The callback interval is 500ms. You can use my.offCompassChange to stop listening.

Parameters

Parameter Type Description
function callback Callback function for the compass data change events.

Callback return value

Parameter Type Description
direction Number The degree between the direction that you are facing and geographical north: [0,360)

Sample code

  1. my.onCompassChange(function (res) {
  2. console.log(res.direction)
  3. })

my.offCompassChange()

Note:
  • This interface is supported since basic library version 1.9.0. Compatibility processing is required for earlier versions. See Mini Program base library to learn more.
  • This interface is only supported in mPaaS 10.1.60 and later versions.

Use this API to unlisten to the compass data.

Sample code

  1. my.offCompassChange()

Whether to pass callback value or not

  • If the callback value is not passed, the callbacks of all events will be removed. The sample code is as follows:

    1. my.offCompassChange();
  • If the callback value is passed, only the corresponding callback is removed. The sample code is as follows:

    1. my.offCompassChange(this.callback);