获取 iOS 中两个坐标之间距离的垂直和水平分量
Get vertical and horizontal components of distance between two coordinates in iOS
我正在尝试在 swift 中创建一个函数,它可以准确计算任何位置距地球原点(纬度:0,经度:0)的垂直和水平距离。我知道 iOS 有 distanceFromLocation 功能,但这给了我直接的位置。我正在寻找的是该方向的水平和垂直分量。我试着想出了自己的解决方案,但是当我根据我得到的水平和垂直分量测试直接距离时,它与实际距离不匹配。这是我的函数:
func distanceFromOrigin(location:CLLocation) {
let lat = location.coordinate.latitude
let lon = location.coordinate.longitude
let earthOriginLocation = CLLocation(coordinate: CLLocationCoordinate2DMake(0.0, 0.0), altitude: CLLocationDistance(0.0), horizontalAccuracy: kCLLocationAccuracyBestForNavigation, verticalAccuracy: kCLLocationAccuracyBestForNavigation, timestamp: NSDate())
var horDistance = earthOriginLocation.distanceFromLocation(CLLocation(latitude: 0.0, longitude: location.coordinate.longitude))
var verDistance = earthOriginLocation.distanceFromLocation(CLLocation(latitude: location.coordinate.latitude, longitude: 0.0))
let overallDistance = earthOriginLocation.distanceFromLocation(location)
if lat < 0 {
print("Object is South of Equator")
verDistance *= -1
} else if lat > 0 {
print("Object is North of Equator")
} else {
print("Object is at the Equator")
}
if lon < 0 {
print("Object is West of Prime Meridian")
horDistance *= -1
} else if lon > 0 {
print("Object is East of Prime Meridian")
} else {
print("Object is at the Prime Meridian")
}
print("Vertical Distance: \(verDistance)")
print("Horizontal Distance: \(horDistance)")
print("Overall Distance: \(overallDistance)")
//Test to see if vertical and horizontal distances are accurate compared to actual distance.
print("Test: \(sqrt((pow(horDistance, 2.0)) + (pow(verDistance, 2.0))))")
}
谢谢!
你的代码没问题,但是你的测试是错误的。
您没有注意到地球不是 平坦的 ,因此您正在考虑的直角三角形位于 大地水准面 上并且有斜边大于平方边和。
我建议您手动执行几个测试,以确保结果看起来逼真,而不是深入研究曲面上的几何体。
小提示:如果点彼此非常接近,您的测试可能通过,因为在这种情况下,地球表面的曲率对计算。
我正在尝试在 swift 中创建一个函数,它可以准确计算任何位置距地球原点(纬度:0,经度:0)的垂直和水平距离。我知道 iOS 有 distanceFromLocation 功能,但这给了我直接的位置。我正在寻找的是该方向的水平和垂直分量。我试着想出了自己的解决方案,但是当我根据我得到的水平和垂直分量测试直接距离时,它与实际距离不匹配。这是我的函数:
func distanceFromOrigin(location:CLLocation) {
let lat = location.coordinate.latitude
let lon = location.coordinate.longitude
let earthOriginLocation = CLLocation(coordinate: CLLocationCoordinate2DMake(0.0, 0.0), altitude: CLLocationDistance(0.0), horizontalAccuracy: kCLLocationAccuracyBestForNavigation, verticalAccuracy: kCLLocationAccuracyBestForNavigation, timestamp: NSDate())
var horDistance = earthOriginLocation.distanceFromLocation(CLLocation(latitude: 0.0, longitude: location.coordinate.longitude))
var verDistance = earthOriginLocation.distanceFromLocation(CLLocation(latitude: location.coordinate.latitude, longitude: 0.0))
let overallDistance = earthOriginLocation.distanceFromLocation(location)
if lat < 0 {
print("Object is South of Equator")
verDistance *= -1
} else if lat > 0 {
print("Object is North of Equator")
} else {
print("Object is at the Equator")
}
if lon < 0 {
print("Object is West of Prime Meridian")
horDistance *= -1
} else if lon > 0 {
print("Object is East of Prime Meridian")
} else {
print("Object is at the Prime Meridian")
}
print("Vertical Distance: \(verDistance)")
print("Horizontal Distance: \(horDistance)")
print("Overall Distance: \(overallDistance)")
//Test to see if vertical and horizontal distances are accurate compared to actual distance.
print("Test: \(sqrt((pow(horDistance, 2.0)) + (pow(verDistance, 2.0))))")
}
谢谢!
你的代码没问题,但是你的测试是错误的。
您没有注意到地球不是 平坦的 ,因此您正在考虑的直角三角形位于 大地水准面 上并且有斜边大于平方边和。
我建议您手动执行几个测试,以确保结果看起来逼真,而不是深入研究曲面上的几何体。
小提示:如果点彼此非常接近,您的测试可能通过,因为在这种情况下,地球表面的曲率对计算。