7
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.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Mbusi Hlatshwayo
  • 554
  • 1
  • 10
  • 23
  • 2
    [KCLError 2](https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CoreLocationConstantsRef/index.html#//apple_ref/c/econst/kCLErrorNetwork) is `kCLErrorNetwork`, described as "The network was unavailable or a network error occurred." May be silly but have you checked your network connection? – Joseph Duffy Mar 16 '15 at 21:57

3 Answers3

7

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.

irodrigo17
  • 198
  • 10
  • 1
    For the sake of completeness The "maximum rate" mentioned in the docs is not specified. While Apple suggests 1 geocoding per minute per app, I managed to do somewhat 100 geocodings one after the other... – Sasho Apr 13 '16 at 07:20
  • @Sasho: sure, but 100 is not a guaranteed rate. If the server has too much requests, it _will_ throttle down your responses. I've seen this more than once when abusing the service. – brainray Jul 06 '16 at 12:58
0

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

Deepak Thakur
  • 3,453
  • 2
  • 37
  • 65
0

Maybe permission is (denied) this easy but make a headache ;