on-compass-change.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. Page({
  2. onShareAppMessage() {
  3. return {
  4. title: '监听罗盘数据',
  5. path: 'packageAPI/pages/device/on-compass-change/on-compass-change'
  6. }
  7. },
  8. data: {
  9. theme: 'light',
  10. enabled: true,
  11. direction: 0
  12. },
  13. onReady() {
  14. const that = this
  15. wx.onCompassChange(function (res) {
  16. that.setData({
  17. direction: parseInt(res.direction, 10)
  18. })
  19. })
  20. },
  21. startCompass() {
  22. if (this.data.enabled) {
  23. return
  24. }
  25. const that = this
  26. wx.startCompass({
  27. success() {
  28. that.setData({
  29. enabled: true
  30. })
  31. }
  32. })
  33. },
  34. stopCompass() {
  35. if (!this.data.enabled) {
  36. return
  37. }
  38. const that = this
  39. wx.stopCompass({
  40. success() {
  41. that.setData({
  42. enabled: false
  43. })
  44. }
  45. })
  46. },
  47. onLoad() {
  48. this.setData({
  49. theme: wx.getSystemInfoSync().theme || 'light'
  50. })
  51. if (wx.onThemeChange) {
  52. wx.onThemeChange(({theme}) => {
  53. this.setData({theme})
  54. })
  55. }
  56. }
  57. })