Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift (Retired) Displaying Our Weather Data The End!

Reverse Geocoding with Swift (weather app)

Hey there,

I finished the iOS with Swift course and managed to let the weather app find and use my location (although I haven't made the app request the authorization but gave it a default value in case it would be denied for now). But I'd like to change the label with the location to the one it finds through my coordinates. After some Googling I found I have to use reverse geocoding and I tried adapting bits of code I found around the web for my purpose but I can't get it to work. Since the part of the course mentioning completionHandlers was a little blurry to me I'm quite lost. Any help ?

My vain attempt :

    func setLocationLabel() -> String! {
        if (CLLocationManager.authorizationStatus() == .NotDetermined ||
            CLLocationManager.authorizationStatus() == .Restricted ||
            CLLocationManager.authorizationStatus() == .Denied) {
                return "Alcatraz, CA"
        } else {
            var locationCoordinates = getLocationCoordinates()
            var geocoder = CLGeocoder()
            var locationCLLocation = CLLocation(latitude: locationCoordinates.latitude, longitude: locationCoordinates.longitude)

            CLGeocoder().reverseGeocodeLocation(locationCLLocation, completionHandler:
                {(placemarks, error) in
                    if (error != nil) {println("reverse geodcode fail: \(error.localizedDescription)")}
                    let pm = placemarks as [CLPlacemark]
                    if pm.count > 0 { showAddPinViewController(placemarks[0] as CLPlacemark) }
            })
        }
    }
}

3 Answers

Enara L. Otaegi
Enara L. Otaegi
13,107 Points

I don't see the startUpdatingLocation() method in your code so maybe that's the problem. I think you need to call this method on the location manager to start searching for the location. And then in the reverse Geocode you need to pass in the location of that same manager as well.

This is from my app and it worked fine.

let geoCoder = CLGeocoder()
var placemark: AnyObject
var error: NSError
geoCoder.reverseGeocodeLocation(manager.location, completionHandler: { (placemark, error) -> Void in
    if error != nil {
    println("Error: \(error.localizedDescription)")
        return
    }
    if placemark.count > 0 {
        let pm = placemark[0] as CLPlacemark
        self.locationLabel.text = "\(pm.locality), \(pm.country)"
    } else {
        println("Error with data")
    }
})

Could it be that I just forgot to declare my placemark variable ? In any case your solution works, thanks!

Vinny Harris-Riviello
Vinny Harris-Riviello
11,898 Points

Antoine did you figure it out? this question is a bit old.

I'm pretty sure the answer is in the frist line using .NotDetermined Enum value, that is always the initial state of the app, you don't need to add this option, if the app is in .NotDetermined status it will prompt the user for authorization, but by you adding this clause here, you are not allowing it to get to the request part of your app and it is going straight into Alcatraz. Another thing to consider (not the reason your code is not rendering the results you expect, but for a cleaner code) You are declaring an instance of Geocoder , but then you use the class itself instead, you might want to check that as well.