如何创建指向目标位置的指南针

时间:2017-01-20 06:20:49

标签: ios swift xcode swift3 compass

我的代码中出现了问题 我的指南针现在通往北方 我希望我的指南针通向位置目标
请帮我解决我的问题。

提前致谢。

这是我的代码

import CoreLocation

        func degreesToRadians(degrees: Float) -> Float {
            return degrees * Float(M_PI) / 180
        }

        private func calculateAngle(userlocation: CLLocation) -> Float {

            let userLocationLatitude: Float = degreesToRadians(degrees: Float(userlocation.coordinate.latitude))
            let userLocationLongitude: Float = degreesToRadians(degrees: Float(userlocation.coordinate.longitude))

            let targetedPointLatitude: Float = degreesToRadians(degrees: 0)//Float(self.latitudeOfTargetedPoint ?? 0))
            let targetedPointLongitude: Float = degreesToRadians(degrees: 0)//Float(self.longitudeOfTargetedPoint ?? 0))

            let longitudeDifference: Float = targetedPointLongitude - userLocationLongitude

            let y: Float = sin(longitudeDifference) * cos(targetedPointLatitude)
            let x: Float = cos(userLocationLatitude) * sin(targetedPointLatitude) - sin(userLocationLatitude) * cos(targetedPointLatitude) * cos(longitudeDifference)
            var radiansValue: Float = atan2(y, x)
            if(radiansValue < 0.0)
            {
                radiansValue += 2 * Float(M_PI)
            }

            return radiansValue
        }

    extension ViewController: CLLocationManagerDelegate {
        func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
            print(newHeading.magneticHeading)
            var direction: Float = Float(newHeading.magneticHeading)

            if direction > 180 {
                direction = 360 - direction
            }
            else {
                direction = 0 - direction
            }
            imageView.transform = CGAffineTransform(rotationAngle: CGFloat(self.degreesToRadians(degrees: direction)))

            distance = Int(direction)
            print("LL : \(direction)")
        }

    }

0 个答案:

没有答案