【问题标题】:Calculate the opposite of the current android GPS walking direction计算当前android GPS行走方向的反方向
【发布时间】:2017-07-17 09:22:00
【问题描述】:

我在这里有这个小代码 sn-p。我要找的是获取用户当前的GPS行走方向(方位),然后得到相反的行走方向。

我似乎在这里遗漏了一些重要信息,因为代码似乎在我向东北和向西走时大部分都可以工作,但在我向南走时却失败了。

知道如何修复我的 sn-p 吗?

private static Location lastValidLocation;

private float getOppositeGPSMovementDirection(Location lastLocation){

    float bearing;
    if (!lastLocation.hasBearing){
        //Use bearing of the last Location with a valid bearing.
        bearing = lastValidLocation.getBearing();
    }else{
        lastValidLocation = lastLocation;
        bearing = lastLocation.getBearing(); 
    }

    //Calculate backwards direction
    int direction = (int) (bearing - 180);
    direction = direction<0?direction*-1:direction;

    return direction;
}

【问题讨论】:

    标签: android gps location bearing


    【解决方案1】:

    你的数学有问题。如果你在 10 度,你的相反是 190。你的数学会给它 170。正确的数学是:

    direction = (bearing + 180) % 360;
    

    基本上,您认为可以将负角乘以 -1 以获得正确角度的假设是错误的。

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      相关资源
      最近更新 更多