错误领域 = kCLErrorDomain,代码 = 2,“操作无法完成。(kCLErrorDomain错误2)。

7
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.)"。我不确定问题出在哪里,有人能看出发生了什么吗?谢谢。


2
KCLError 2kCLErrorNetwork,描述为“网络不可用或发生网络错误。”可能有点傻,但您检查过您的网络连接了吗? - Joseph Duffy
3个回答

7

这是一个网络错误,根据文档,CLGeocoder需要一个正常的网络连接才能反向地编码位置。

此外,如果您超过请求速率,CLGeocoder会限制地理编码请求,并返回同样的错误,这也在类参考中有所说明。


1
为了完整起见,文档中提到的“最大速率”未进行具体说明。虽然苹果建议每个应用程序每分钟进行1次地理编码,但我已成功完成连续进行约100次地理编码... - Sasho
@Sasho:当然可以,但是100并不是一个保证的速率。如果服务器收到太多请求,它会限制你的响应速度。我在滥用服务时已经看到过这种情况不止一次了。 - brainray

0

或者,您可以将经纬度传递给此{{link1:Google API}},并根据JSON中的“类型”获取与地址相关的所有字段。


0

也许权限被拒绝了,这很简单但会让人头疼;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接