swift Error Domain=kCLErrorDomain Code=2“无法完成操作,(kCLErrorDomain错误2,)”

5q4ezhmt  于 2022-12-10  发布在  Swift
关注(0)|答案(3)|浏览(147)
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“无法完成作业。(kCLErrorDomain error 2.)”。我不确定是什麽问题,有人可以看到发生了什麽事吗?谢谢。

yqkkidmi

yqkkidmi1#

这是一个网络错误,CLGeocoder需要一个有效的网络连接,以便根据the docs对位置进行反向地理编码。
此外,CLGeocoder将限制地理编码请求,如果超过请求速率,则返回相同的错误,这也在类参考中进行了说明。

xkrw2x1b

xkrw2x1b2#

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

rsl1atfo

rsl1atfo3#

也许许可(被拒绝)这容易但令人头痛;

相关问题