ios - 错误域=kCLErrorDomain 代码=2 "The operation couldn’ t 完成。 (kCLErrorDomain 错误 2。)”

标签 ios swift

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet var latLabel: UILabel!
    @IBOutlet var longLabel: UILabel!

    @IBOutlet var courseLabel: UILabel!
    @IBOutlet var speedLabel: UILabel!
    @IBOutlet var altLabel: UILabel!
    @IBOutlet var addressLabel: UILabel!

    var manager:CLLocationManager!
    var userLocation:CLLocation = CLLocation()

    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.distanceFilter = 50
        manager.startUpdatingLocation()


    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        userLocation = locations[0] as CLLocation
        println(userLocation.coordinate.latitude)

        var latitude:CLLocationDegrees = userLocation.coordinate.latitude
        latLabel.text = "\(latitude)"
        var longitude:CLLocationDegrees = userLocation.coordinate.longitude
        longLabel.text = "\(longitude)"

        var course:CLLocationDirection = userLocation.course
        courseLabel.text = "\(course)"

        var speed:CLLocationSpeed = userLocation.speed
        speedLabel.text = "\(speed)"

        var altitude:CLLocationAccuracy = userLocation.altitude
        altLabel.text = "\(altitude)"


        CLGeocoder().reverseGeocodeLocation(userLocation, completionHandler: { (placemarks, error) -> Void in

            if (error != nil) {

                println(error)

            } else {
                if let p = CLPlacemark(placemark: placemarks?[0] as CLPlacemark) {
                    println(p)
                }
            }

        })



        //println("Location = \(locations)")
        println(locations)
    }


}

当我尝试获取用户最近的地址时,我不断收到错误 Error Domain=kCLErrorDomain Code=2 "The operation couldn't be completed. (kCLErrorDomain error 2.)"。我不确定问题是什么,任何人都可以看到发生了什么吗?谢谢。

最佳答案

这是一个网络错误,根据 the docs,CLGeocoder 需要有效的网络连接才能对位置进行反向地理编码。 .

此外,CLGeocoder 将限制地理编码请求,如果您超过请求速率,将返回相同的错误,这也记录在类引用中。

关于ios - 错误域=kCLErrorDomain 代码=2 "The operation couldn’ t 完成。 (kCLErrorDomain 错误 2。)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29087660/

相关文章:

ios - 对成员 'downloadTask(with:completionHandler:)' 的模糊引用

ios - 从 AppDelegate 自定义每个 UISearchBar 不工作

objective-c - 如何制作一个移动按钮?

iphone - 计算并显示在标签中

iOS stringByPaddingToLength 因心脏字符而失败

ios - NSData 到 Struct 搞乱数据

ios - NotificationServiceExtension 上的 Firebase 存储配置错误

ios - 这是在闭包中使用 weak self 的正确位置吗?

ios - CAGradientLayer 不显示

ios - Kingfisher 3.0 iOS swift RoundCornerImageProcessor 白色背景