小程序开发之通过加速度计判断手机是否移动和微信摇一摇功能

记录:因为本公司是做定位,导航相关的业务,小程序需要实时的从后台查询位置数据进行显示,查询之前需要做一个手机是否在移动的判断,其实这个功能和摇一摇功能实现逻辑是一个道理,查阅资料,具体实现如下:


wx.onAccelerometerChange(CALLBACK)


监听加速度数据,频率:5次/秒,接口调用后会自动开始监听,可使用 wx.stopAccelerometer 停止监听。

CALLBACK返回参数:

参数  类型  说明
 x  Number  X 轴
 y  Number  Y 轴
 z  Number  Z 轴


功能实现代码:

    //判断手机是否移动
    wx.onAccelerometerChange(function (e) {
      if (Math.abs(e.x) > 1.1 && Math.abs(e.y) > 1.1){
        wx.showToast({ title: "摇一摇" })
        that.data.move = 2;
      }else if (Math.abs(e.x) > 0.07 && Math.abs(e.y) > 0.02) {
        wx.showToast({ title: "移动" })
        that.data.move = 1;
      } else {
        wx.showToast({ title: "静止" })
        that.data.move = 0;
      }   
    })


猜你喜欢

转载自blog.csdn.net/qq_37936542/article/details/80481565