Error Domain=kCLErrorDomain Code=2 “The operation couldn’t be completed. (kCLErrorDomain error 2.)”

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

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.

回答1:

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.



回答2:

Alternatively, you can pass lat long to this google api and get all fields related to address depending on the "types" in the json



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!