【问题标题】:How to tell user accurate degrees to face north or east?如何告诉用户准确的度数是朝北还是朝东?
【发布时间】:2019-08-09 16:49:00
【问题描述】:

我正在创建一个导航应用程序。我想知道我当前的航向和东方之间的度数。我这样做的方式是用角度 0 减去真实航向,如果是北,则为 90 度,以此类推。当差异达到let i: ClosedRange<Double> = 0...20 时,我猜测航向朝向预期的方向,在本例中为东方。

我想知道这是否是完美的方法。如果我应该使用轴承,我仍然感到困惑。

  //calculate the difference between two angles ( current heading and east angle, 90 degrees)

    func cal(firstAngle: Double) -> Double {
        var diff = heading - 90
        if diff < -360 {
            diff += 360
        } else if diff > 360 {
            diff -= 360
        }
        return diff
    }
// check if the difference falls in the range
let i: ClosedRange<Double> = 0...20

if !(i.contains(k)) {
    k = cal(firstAngle: b)
    } else if (i.contains(k)) {
    let message = "You are heading east"
     print(message)
      } else {return}
   }
  func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        var heading = newHeading.trueHeading }

【问题讨论】:

    标签: swift navigation maps


    【解决方案1】:

    这应该可以满足您的需求。代码中的注释:

    func cal(heading: Double, desired: Double) -> Double {
        // compute adjustment
        var angle = desired - heading
    
        // put angle into -360 ... 360 range
        angle = angle.truncatingRemainder(dividingBy: 360)
    
        // put angle into -180 ... 180 range
        if angle < -180 {
            angle += 360
        } else if angle > 180 {
            angle -= 360
        }
    
        return angle
    }
    
    // some example calls
    cal(heading: 90, desired: 180)  // 90
    cal(heading: 180, desired: 90)  // -90
    cal(heading: 350, desired: 90)  // 100
    cal(heading: 30, desired: 270)  // -120 
    
    let within20degrees: ClosedRange<Double> = -20...20
    
    let adjust = cal(heading: 105, desired: 90)
    if within20degrees ~= adjust {
        print("heading in the right direction")
    }
    
    heading in the right direction
    

    【讨论】:

    • 谢谢,先生!这就是我一直在努力的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多