【问题标题】:HTML5/Javascript calculate device speed using devicemotion/deviceorientationHTML5/Javascript 使用 devicemotion/deviceorientation 计算设备速度
【发布时间】:2015-11-12 14:10:37
【问题描述】:

是否可以使用 devicemotion/deviceorientation HTML5 API 以 km/h 为单位计算设备速度?

我想知道用户是否在走路/跑步/不动,我不能使用地理定位 API,因为它也必须在建筑物内工作。

【问题讨论】:

    标签: javascript html devicemotion


    【解决方案1】:

    当然可以。您从 devicemotion 事件中获得的加速度以 m/s² 为单位。

    var lastTimestamp;
    var speedX = 0, speedY = 0, speedZ = 0;
    window.addEventListener('devicemotion', function(event) {
      var currentTime = new Date().getTime();
      if (lastTimestamp === undefined) {
        lastTimestamp = new Date().getTime();
        return; //ignore first call, we need a reference time
      }
      //  m/s² / 1000 * (miliseconds - miliseconds)/1000 /3600 => km/h (if I didn't made a mistake)
      speedX += event.acceleration.x / 1000 * ((currentTime - lastTimestamp)/1000)/3600;
      //... same for Y and Z
      lastTimestamp = currentTime;
    }, false);
    

    应该这样做。但我会小心,因为手机中的加速度计不太准确;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      相关资源
      最近更新 更多