【问题标题】:Error Domain=kCLErrorDomain Code=2 "The operation couldn’t be completed. (kCLErrorDomain error 2.)"Error Domain=kCLErrorDomain Code=2 "操作无法完成。(kCLErrorDomain error 2.)"
【发布时间】:2015-05-19 04:57:54
【问题描述】:
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 could not be completed. (kCLErrorDomain error 2.)"。我不确定问题是什么,有人可以看到发生了什么吗?谢谢。

【问题讨论】:

  • KCLError 2 是kCLErrorNetwork,描述为“网络不可用或发生网络错误”。可能有点傻,但您检查过您的网络连接吗?

标签: ios swift


【解决方案1】:

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

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

【讨论】:

  • 为了完整起见,未指定文档中提到的“最大速率”。虽然 Apple 建议每个应用每分钟进行 1 次地理编码,但我设法一个接一个地进行了大约 100 次地理编码......
  • @Sasho:当然,但 100 不是保证率。如果服务器有太多的请求,它会限制你的响应。我在滥用服务时不止一次看到过这种情况。
【解决方案2】:

或者,您可以将 lat long 传递给 google api 并根据 json 中的“类型”获取与地址相关的所有字段

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 2013-10-21
    • 2014-01-04
    • 2010-11-24
    • 1970-01-01
    • 2012-12-24
    • 2015-06-03
    • 2014-12-01
    • 2021-09-28
    相关资源
    最近更新 更多