Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

ios - Error Domain=kCLErrorDomain Code=2 "The operation couldn’t be completed. (kCLErrorDomain error 2.)"

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)
    }


}

I keep getting this error Error Domain=kCLErrorDomain Code=2 "The operation couldn’t be completed. (kCLErrorDomain error 2.)" when I try to get the users closest address. I am not sure what the issue is, can anybody see what is going on? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's a network error, CLGeocoder needs a working network connection in order to reverse geocode a location, according to the docs.

Also, CLGeocoder will throttle geocoding requests, returning that same error if you exceed the request rate, this is also documented in the class reference.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...