on-compass-change.js 788 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. Page({
  2. onShareAppMessage() {
  3. return {
  4. title: '监听罗盘数据',
  5. path: 'page/API/pages/on-compass-change/on-compass-change'
  6. }
  7. },
  8. data: {
  9. enabled: true,
  10. direction: 0
  11. },
  12. onReady() {
  13. const that = this
  14. wx.onCompassChange(function (res) {
  15. that.setData({
  16. direction: parseInt(res.direction, 10)
  17. })
  18. })
  19. },
  20. startCompass() {
  21. if (this.data.enabled) {
  22. return
  23. }
  24. const that = this
  25. wx.startCompass({
  26. success() {
  27. that.setData({
  28. enabled: true
  29. })
  30. }
  31. })
  32. },
  33. stopCompass() {
  34. if (!this.data.enabled) {
  35. return
  36. }
  37. const that = this
  38. wx.stopCompass({
  39. success() {
  40. that.setData({
  41. enabled: false
  42. })
  43. }
  44. })
  45. }
  46. })