【问题标题】:CLLocationManager returning Nil valueCLLocationManager 返回 Nil 值
【发布时间】:2015-09-28 22:14:59
【问题描述】:

我正在尝试使用 CLLocationManager 以两个坐标的形式返回用户位置。

  • 我认为我有适当的授权 (authorizedWhenInUse),但在获取实际位置时,它返回 nil

  • 我的 plist 中也添加了 NSLocationWhenInUseUsageDescription

    import UIKit
    import CoreLocation    
    class ViewController: UIViewController, CLLocationManagerDelegate {  
    // Create an instance of the CLLocationManager
        var locManager = CLLocationManager()
    
    // View did load function (run things inside of here)
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        locManager.delegate = self
        locManager.desiredAccuracy = kCLLocationAccuracyBest
    
        // Check authorizationStatus
        let authorizationStatus = CLLocationManager.authorizationStatus()
        // List out all responses
        switch authorizationStatus {
        case .AuthorizedAlways:
            println("authorized")
        case .AuthorizedWhenInUse:
            println("authorized when in use")
        case .Denied:
            println("denied")
        case .NotDetermined:
            println("not determined")
        case .Restricted:
            println("restricted")
        }
    
        // Get the location
        locManager.startUpdatingLocation()
    
        if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){
            // Extract the location from CLLocationManager
            let userLocation = locManager.location
    
            // Check to see if it is nil
            if userLocation != nil {
                println("location is \(userLocation)")
            } else {
                println("location is nil")
            }
        } else {
            println("not authorized")
        }
    
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    }

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    SDK 中的 CLLocationManager.h 头文件提供了有关“.location”属性的有用提示:

    Discussion:
     *      The last location received. Will be nil until a location has been received.
    

    最好的办法是实现:

    optional func locationManager(_ manager: CLLocationManager,
           didUpdateLocations locations: [CLLocation])
    

    委托方法并从那里获取您的位置,当设备的 GPS 解析出手机所在的位置时会调用该方法。

    类似:

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!) {
    
        let userLocation = manager.location
    
        print("location = \(userLocation.latitude) \(userLocation.longitude)")
    
      }
    

    【讨论】:

    • 我知道如果没有确定位置会是Nil,但是为什么locManager.startUpdatingLocation()没有提供位置呢?
    • 解析位置确实需要几秒钟。此外,您应该实现"locationManager(_:didFailWithError:)" delegate method 让您知道什么时候出现故障(例如,用户没有授予使用位置管理器的权限?)
    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多