【问题标题】:Calculating the cardinal direction of a smartphone with JS用JS计算智能手机的基本方向
【发布时间】:2020-04-21 06:06:16
【问题描述】:

在 JavaScript 中,我们可以获得设备的 alpha、beta 和 gamma 角度。

  • alpha:围绕 z 轴 [0, 360] 旋转
  • beta:绕 x 轴旋转 [90, -90]
  • gamma:绕 y 轴旋转 [180, -180]

window.addEventListener('deviceorientation', (event)=>{

    const alpha = event.alpha; // Returns the rotation of the device around the Z axis
    const beta = event.beta;  // Returns the rotation of the device around the X axis; that is, the number of degrees, ranged between -180 and 180
    const gamma = event.gamma; // Returns the rotation of the device around the Y axis; that is, the number of degrees, ranged between -90 and 90

    console.log(`alpha: ${alpha}, beta: ${beta}, gamma: ${gamma}`);

}, false);

但是,我想使用这些角度来确定设备的基本方向(北、西、东和南)。

我的问题:
是否可以只使用 alpha 角来确定设备的基本方向,还是应该同时使用 beta 和 gamma?
如果需要 beta 和 gamma;为了计算基本方向,我应该如何在计算中使用它们,有没有我应该知道的公式?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    除了@AugustJelemson 在他的代码中提到的所有事情,如果你想从指南针方向获得实际的基本方向,你可以简单地做

    const direction = ['north','north east', 'east','south east', 'south','south west', 'west','north west'][Math.floor(((angle+22.5)%360)/45)]
    

    0 ~ 22.5338.5 ~ 360的角度,direction将被设置为north,在22.5~68.5,它将被设置为north east,以此类推。


    此外,您测试程序的浏览器将极大地改变您的结果。使用 IOS 设备很可能会破坏您的程序(即使使用 webkitCompassHeading 也很不准确)。

    而且我个人觉得你可以简单地使用event.alphaevent.webkitCompassHeading 作为你的角度而没有任何问题,尽管August 所做的是一种更安全的方法。

    干杯:)

    【讨论】:

      【解决方案2】:
      var heading;
      
      window.addEventListener('deviceorientation', handleOrientation, false);
      
      const handleOrientation = (event) => {
          if(event.webkitCompassHeading) {
              // some devices don't understand "alpha" (especially IOS devices)
              heading = event.webkitCompassHeading;
          }
          else{
              heading = compassHeading(event.alpha, event.beta, event.gamma);
          }
      };
      
      const compassHeading = (alpha, beta, gamma) => {
      
          // Convert degrees to radians
          const alphaRad = alpha * (Math.PI / 180);
          const betaRad = beta * (Math.PI / 180);
          const gammaRad = gamma * (Math.PI / 180);
      
          // Calculate equation components
          const cA = Math.cos(alphaRad);
          const sA = Math.sin(alphaRad);
          const cB = Math.cos(betaRad);
          const sB = Math.sin(betaRad);
          const cG = Math.cos(gammaRad);
          const sG = Math.sin(gammaRad);
      
          // Calculate A, B, C rotation components
          const rA = - cA * sG - sA * sB * cG;
          const rB = - sA * sG + cA * sB * cG;
          const rC = - cB * cG;
      
          // Calculate compass heading
          let compassHeading = Math.atan(rA / rB);
      
          // Convert from half unit circle to whole unit circle
          if(rB < 0) {
              compassHeading += Math.PI;
          }else if(rA < 0) {
              compassHeading += 2 * Math.PI;
          }
      
          // Convert radians to degrees
          compassHeading *= 180 / Math.PI;
      
          return compassHeading;
      };
      
      

      上面这段代码的灵感来自这些链接:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-08
        相关资源
        最近更新 更多