orientation.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <view class="uni-btn-v">
  6. <button type="primary" @tap="getOrient">获取设备的方向信息</button>
  7. <button type="primary" @tap="watchOrient">监听设备的方向变化</button>
  8. <button type="primary" @tap="watchStop">停止监听</button>
  9. </view>
  10. <view class="uni-textarea">
  11. <textarea :value="value" />
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. var id = null
  18. export default {
  19. data() {
  20. return {
  21. title: 'orientation',
  22. value: ''
  23. }
  24. },
  25. onUnload() {
  26. this.watchStop();
  27. },
  28. methods: {
  29. getOrient: function () {
  30. var that = this;
  31. plus.orientation.getCurrentOrientation(function (o) {
  32. that.value = "alpha:" + o.alpha + "\nbeta:" + o.beta + "\ngamma:" + o.gamma;
  33. }, function (e) {
  34. console.log("获取失败:" + e.message);
  35. });
  36. },
  37. watchOrient: function () {
  38. var that = this;
  39. if (id) {
  40. return;
  41. }
  42. id = plus.orientation.watchOrientation(function (o) {
  43. that.value = "监听设备方向变化信息\n" + "alpha:" + o.alpha + "\nbeta:" + o.beta + "\ngamma:" + o.gamma;
  44. }, function (e) {
  45. plus.orientation.clearWatch(id);
  46. id = null;
  47. console.log("监听失败:" + e.message);
  48. });
  49. },
  50. watchStop: function () {
  51. if (id) {
  52. plus.orientation.clearWatch(id);
  53. id = null;
  54. } else {
  55. console.log("没有监听设备方向变化");
  56. }
  57. }
  58. }
  59. }
  60. </script>
  61. <style>
  62. </style>