Kil*_*ian 0 core-location ios swift
我有我的位置(坐标)和我的航向(指南针),并且正在尝试计算朝向给定其他坐标的航向,以便我可以在屏幕上显示指向该位置的箭头。我正在构建的基本上是一个美化的指南针,它不指向北方,而是指向特定的坐标,因此需要考虑该坐标的方位和用户的指南针方向。
\n我期望 CoreLocation 包含为此所需的构建块,但除了获取两个坐标之间的距离之外,似乎没有任何东西。
\n不幸的是,我手动计算的尝试并没有取得很大成果,而且很难猜测我哪里出错了。此后,我尝试调整我在此处找到的内容,并且我在那里的示例数据中得到了相同的结果,但一旦我使用自己的坐标,它就会完全出错。
\nextension Double {\n var radians: Double {\n self * .pi / 180\n }\n}\n\n\nlet A = CLLocationCoordinate2D(latitude: 51.0295437, longitude: 13.7277793)\nlet B = CLLocationCoordinate2D(latitude: 51.026819, longitude: 13.726348)\n\nlet \xce\x94L = abs(A.longitude) - abs(B.longitude)\nlet X = cos(B.latitude.radians) * sin(\xce\x94L.radians)\nlet Y = cos(A.latitude.radians) * sin(B.latitude.radians) - sin(A.latitude.radians) * cos(B.latitude.radians) * cos(\xce\x94L.radians)\nlet bearing = atan2(X, Y)\nlet heading = bearing - userHeading\nRun Code Online (Sandbox Code Playgroud)\n一旦我将设备的标题合并到此(最后的减法)中,似乎我的值以某种方式沿 WE 轴翻转(指向左而不是右,反之亦然),但是 NS 轴似乎是正确的。方位角的计算肯定有问题。\n我真的对地理坐标系统了解不多。
\n我们这样试试吧。首先我需要建立一些转换:
extension Double {
var toRadians : Double {
var m = Measurement(value: self, unit: UnitAngle.degrees)
m.convert(to: .radians)
return m.value
}
var toDegrees : Double {
var m = Measurement(value: self, unit: UnitAngle.radians)
m.convert(to: .degrees)
return m.value
}
}
Run Code Online (Sandbox Code Playgroud)
现在我将制定一个公式;a是我所在的位置,b是我想要其方位的遥远点:
let a = // some CLLocationCoordinate2D
let b = // some CLLocationCoordinate2D
let deltaL = b.longitude.toRadians - a.longitude.toRadians
let thetaB = b.latitude.toRadians
let thetaA = a.latitude.toRadians
let x = cos(thetaB) * sin(deltaL)
let y = cos(thetaA) * sin(thetaB) - sin(thetaA) * cos(thetaB) * cos(deltaL)
let bearing = atan2(x,y)
let bearingInDegrees = bearing.toDegrees
print(bearingInDegrees) // sanity check
Run Code Online (Sandbox Code Playgroud)
现在我将测试一些实际的轴承。我将给出a并b跟随打印的内容 ( bearingInDegrees)。我将从我们获得公式的页面开始:
let a = CLLocationCoordinate2D(latitude: 39.099912, longitude: -94.581213)
let b = CLLocationCoordinate2D(latitude: 38.627089, longitude: -90.200203)
// 96.51262423499941, yep, that's the answer given on example page
Run Code Online (Sandbox Code Playgroud)
好的,成功了。现在我将尝试自己以及我在不同方向上的一些观点:
let a = CLLocationCoordinate2D(latitude: 34.439931, longitude: -119.263984)
let b = CLLocationCoordinate2D(latitude: 34.489290, longitude: -119.221670)
// 35 degrees, about right
let a = CLLocationCoordinate2D(latitude: 34.439931, longitude: -119.263984)
let b = CLLocationCoordinate2D(latitude: 34.484479, longitude: -119.308273)
// -40 degrees, about right
let a = CLLocationCoordinate2D(latitude: 34.439931, longitude: -119.263984)
let b = CLLocationCoordinate2D(latitude: 34.376978, longitude: -119.329645)
// -139, about right
Run Code Online (Sandbox Code Playgroud)
好的,让我们在其他经度值为正的位置尝试一下:
let a = CLLocationCoordinate2D(latitude: 52.518044, longitude: 13.374602) // Berlin
let b = CLLocationCoordinate2D(latitude: 53.444748, longitude: 14.534668) // Szczecin
// 36, looks good to me
Run Code Online (Sandbox Code Playgroud)
美好的!现在让我们填写拼图的最后一块,即我实际面对的方位。我认为以度为单位,所以我们继续使用度。我以最后的结果为例。我在柏林,但现在我面朝东方。将我的设备顶部称为零。针应该指向什么角度才能指向什切青?
let myHeading = 90.0 // (I'm facing east)
let bearingFromMe = bearingInDegrees - myHeading
print(bearingFromMe) // -53, sounds good
Run Code Online (Sandbox Code Playgroud)
-53意思是向前和我的左边。果然,我面朝东方,所以那就是我们要去的方向。
| 归档时间: |
|
| 查看次数: |
1351 次 |
| 最近记录: |