问题描述
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)
}
}
我一直收到此错误错误域= kCLErrorDomain Code = 2操作无法完成。(kCLErrorDomain error 2 。)当我试图获得用户最近的地址时。我不确定问题是什么,有人能看到发生了什么吗?谢谢。
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.
推荐答案
这是一个网络错误,CLGeocoder需要一个有效的网络连接才能反转地理位置,根据。
That's a network error, CLGeocoder needs a working network connection in order to reverse geocode a location, according to the docs.
此外,CLGeocoder会限制地理编码请求,如果超出请求率则返回相同的错误,这也在类引用中记录。
Also, CLGeocoder will throttle geocoding requests, returning that same error if you exceed the request rate, this is also documented in the class reference.
这篇关于错误域= kCLErrorDomain代码= 2“操作无法完成。 (kCLErrorDomain错误2。)“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!