Dav*_*vid 4 angle core-location coordinates augmented-reality
这是计算从一个 CLLocation 到另一个 CLLocation 的角度(以弧度为单位)的有效方法吗?
-(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;}
Run Code Online (Sandbox Code Playgroud)
请指教!
任何帮助都感激不尽。
斯威夫特 5 版本:
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
extension CLLocationCoordinate2D {
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
}
}
}
Run Code Online (Sandbox Code Playgroud)