ios - 无法在指南针 Swift ios 中指向正确的位置
问题描述
我正在开发一个指南针,它可以从我当前的位置指向其他位置,但是在实现它之后,我注意到每次选择一个新的目标位置去或指向时它总是指向同一个方向。
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let heading = newHeading.magneticHeading
let headingR = Measurement(value: newHeading.trueHeading, unit: UnitAngle.degrees).converted(to: .radians).value
// To know if the Data that our device is correct, The "horizontalAccuracy" value should be grater then ZERO for valid GPS coordinate Data
if heading < 0 {return}
// Get the heading(direction)
if(heading == -1){
print("no location")
}
self.directionAngel = Double(self.getBearingBetweenTwoPoints1(latDestination: self.desiredLatit,lonDestination: self.desiredLongit))
let north = -1 * heading.degreesToRadians
UIView.animate(withDuration: 1) {
self.imageView.transform = CGAffineTransform(rotationAngle: CGFloat(north))
self.directionArrowView.transform = CGAffineTransform(rotationAngle: CGFloat(self.directionAngel.degreesToRadians - headingR))
}
self.updateHeading(heading: heading)
}
private func getBearingBetweenTwoPoints1(latDestination:Double,lonDestination:Double) -> Double {
let lat1 = getMyLocation().latitude.degreesToRadians
let lon1 = getMyLocation().longitude.degreesToRadians
let lat2 = latDestination.degreesToRadians
let lon2 = lonDestination.degreesToRadians
let dLon = lon2 - lon1
let y = sin(dLon) * cos(lat2)
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)
var radiansBearing = atan2(y, x)
if(radiansBearing < 0.0){
radiansBearing += 2 * Double.pi
}
return radiansBearing
}
let locationManager: CLLocationManager = {
$0.requestAlwaysAuthorization()
$0.requestWhenInUseAuthorization()
$0.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
$0.startUpdatingLocation()
$0.startUpdatingHeading()
return $0
}(CLLocationManager())
解决方案
推荐阅读
- javascript - 多个目录上的 Glob 同步模式
- javascript - Fullcalendar popover 错误:引导工具提示需要 Popper.js,尽管这已经包含在内
- scala - 如何高效地编写这个函数?
- android - 活动意外停止而没有崩溃
- mysql - mysql; 如何分析仅在一个数据库中发生的慢查询
- git - 创建 Symfony 供应商 github 包的分支
- json - 某些字段上的杰克逊注释混合不起作用
- python - 我收到来自同一邮件域的邮件,该邮件域是 django 中的主机邮件
- regex - 正则表达式不适用于在 word 文档的范围内查找
- php - 使用 Mysqli 和准备好的语句进行分页