【问题标题】:Finding the compass orientation of an iPhone in swift快速查找 iPhone 的指南针方向
【发布时间】:2015-08-22 15:56:45
【问题描述】:

我正在尝试制作一个类似于指南针的应用程序,但有显着差异,但它们并不重要。我需要知道的是:无论手机的方向如何,我如何让 iPhone 给我指南针方向(即北方为 0 度)(即,如果它平放在桌子上或肖像中,它会给出相同的读数某人的手,如果它指向相同的方向)

TL;DR 我怎样才能让 iPhone 绕 y 轴旋转大约每秒更新一次。

【问题讨论】:

    标签: ios iphone swift location compass


    【解决方案1】:

    import CoreLocation

    class ViewController: UIViewController ,CLLocationManagerDelegate {
    
        var lm:CLLocationManager!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            lm = CLLocationManager()
            lm.delegate = self
    
            lm.startUpdatingHeading()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) {
            println(newHeading.magneticHeading)
        }
    }
    

    您可以从https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLHeading_Class/获取更多信息

    【讨论】:

    • 我在 sim 和设备上都试过了,但我无法显示任何标题值。我把它绑在一个标签上,当我在外面走动时应该更新。有什么想法吗?
    • 很好的答案,但我想不出实现didReceiveMemoryWarning() 的理由,只需调用super — 我的意思是,这是默认行为。
    【解决方案2】:

    斯威夫特 3:

    import UIKit
    import CoreLocation
    
    class ViewController: UIViewController, CLLocationManagerDelegate {
    
        let locationManager = CLLocationManager()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
           // Azimuth
            if (CLLocationManager.headingAvailable()) {
                locationManager.headingFilter = 1
                locationManager.startUpdatingHeading()
                locationManager.delegate = self
            }
        }
    
        func locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
            print (heading.magneticHeading)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

    【讨论】:

    • 我的代码与您在我的应用程序中显示的完全一样,但在打开位置路径的模拟器中,我从未打印出任何标题
    • 模拟器不支持磁航向,在真机上试试。
    【解决方案3】:

    Swift 3.0

        import UIKit
        import CoreLocation
    
        class ViewController: UIViewController, CLLocationManagerDelegate {
    
        @IBOutlet weak var compass: UIImageView!
        @IBOutlet weak var angleLabel: UILabel!
        @IBOutlet weak var geographicalDirectionLabel: UILabel!
    
        var locationManager = CLLocationManager()
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
    
    
            locationManager.delegate = self
    
            // Start location services to get the true heading.
            locationManager.distanceFilter = 1000
            locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
            locationManager.startUpdatingLocation()
    
            //Start heading updating.
            if CLLocationManager.headingAvailable() {
                locationManager.headingFilter = 5
                locationManager.startUpdatingHeading()
            }
    
    
    
    
        }
    
        func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    
            if newHeading.headingAccuracy < 0 {
                return
            }
    
            // Get the heading(direction)
            let heading: CLLocationDirection = ((newHeading.trueHeading > 0) ?
                newHeading.trueHeading : newHeading.magneticHeading);
            UIView.animate(withDuration: 0.5) {
                let angle = CGFloat(heading).toRadians // convert from degrees to radians
                self.compass.transform = CGAffineTransform(rotationAngle: angle) // rotate the picture
            }
            print(heading)
            angleLabel.text = String(format: "%0.2f", heading)
    
           var strDirection = String()
            if(heading > 23 && heading <= 67){
                strDirection = "North East";
            } else if(heading > 68 && heading <= 112){
                strDirection = "East";
            } else if(heading > 113 && heading <= 167){
                strDirection = "South East";
            } else if(heading > 168 && heading <= 202){
                strDirection = "South";
            } else if(heading > 203 && heading <= 247){
                strDirection = "South West";
            } else if(heading > 248 && heading <= 293){
                strDirection = "West";
            } else if(heading > 294 && heading <= 337){
                strDirection = "North West";
            } else if(heading >= 338 || heading <= 22){
                strDirection = "North";
            }
    
            geographicalDirectionLabel.text = strDirection
        }
      }
    

    【讨论】:

    • 感谢您的回答,trueHeading 是否提供与北方相关的实际价值?
    猜你喜欢
    • 2011-05-07
    • 2016-06-02
    • 1970-01-01
    • 2011-05-22
    • 2011-12-05
    • 1970-01-01
    相关资源
    最近更新 更多