计算2个CLLocations之间的角度的有效方法? [英] valid way to calculate angle between 2 CLLocations?
本文介绍了计算2个CLLocations之间的角度的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一种计算从一个CLLocation到另一个CLLocation的角度(以弧度为单位)的有效方法吗?
Is this a valid way to calculate an angle (in radians) from one CLLocation to another?
-(float)angleFromLocation:(CLLocationCoordinate2D)start toLocation:(CLLocationCoordinate2D)end {
float deltaX = start.latitude - end.latitude;
float deltaY = start.longitude - end.longitude;
float ang = atan2(deltaY, deltaX);
return ang;}
请告知!
任何帮助将不胜感激.
推荐答案
Swift 4版本:
Swift 4 version:
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
extension CLLocationCoordinate2D: Equatable {
func heading(to: CLLocationCoordinate2D) -> Double {
let lat1 = self.latitude.degreesToRadians
let lon1 = self.longitude.degreesToRadians
let lat2 = to.latitude.degreesToRadians
let lon2 = to.longitude.degreesToRadians
let dLon = lon2 - lon1
let y = sin(dLon) * cos(lat2)
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)
let headingDegrees = atan2(y, x).radiansToDegrees
if headingDegrees >= 0 {
return headingDegrees
} else {
return headingDegrees + 360
}
}
}
这篇关于计算2个CLLocations之间的角度的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文