将学位转换为iOS学位系统

时间:2019-08-20 06:42:21

标签: ios swift math geometry

所以我试图使用

从两个位置坐标中找到方位角

https://www.sunearthtools.com/tools/distance.php

输入

坐标A:21.642534、69.607003, 坐标B:21.642083,69.614587

输出为93.66度

enter image description here

现在我想在圈子上显示此内容,所以我正在使用以下代码

    let view = UIView(frame: self.view.bounds)
    self.view.addSubview(view)


    let circle = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width - 20, height: view.frame.width - 20))
    view.addSubview(circle)
    circle.center = view.center
    circle.layer.cornerRadius = circle.frame.height / 2
    circle.backgroundColor = UIColor.gray.withAlphaComponent(0.7)




    let dotView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    dotView.layer.cornerRadius = dotView.frame.width / 2
    dotView.backgroundColor = UIColor.purple.withAlphaComponent(0.89)
    dotView.center = circle.center
    circle.addSubview(dotView)


    let location1 = CLLocation(latitude: 21.642534, longitude: 69.607003)
    let location2 = CLLocation(latitude: 21.642083, longitude: 69.614587)

    let angleStep = CGFloat(location1.bearingRadianTo(location: location2))
    print(location1.bearingDegreesTo(location: location2))
    let xPos = cos(angleStep) * (circle.frame.width / 2)
    let yPos = sin(angleStep) * (circle.frame.width / 2)

    dotView.center = CGPoint(x:circle.center.x + xPos - circle.frame.origin.x, y:circle.center.y + yPos - circle.frame.origin.y);

输出为紫色点在底部,应该在右侧,就像第一个图像一样。

我已验证我在angleStep中获得了正确的价值

经过一番谷歌搜索后,我发现iOS拥有不同的学位系统

enter image description here

enter image description here

如何将学位系统转换为iOS学位系统,以便在右侧显示紫色圆圈?

预先感谢

1 个答案:

答案 0 :(得分:2)

现实世界的头向北使用0度(向上)。但是,如您上一张图中所示,iOS坐标系中的0度位于右侧,而不是顶部。您需要做的就是从angleStep中减去π/ 2弧度,以将“真实”标题转换为“ iOS”标题。

let angleStep = CGFloat(location1.bearingRadianTo(location: location2)) - CGFloat.pi / 2