如何解决无法在罗盘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())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。