26

My input is a latitude and longitude. I need to use the reverseGeocodeLocation function of swift, to give me the output of the locality. The code I have tried to use is

            println(geopoint.longitude) 
            println(geopoint.latitude)
            var manager : CLLocationManager!
            var longitude :CLLocationDegrees = geopoint.longitude
            var latitude :CLLocationDegrees = geopoint.latitude

            var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
            println(location)

            CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in
                println(manager.location)

                if error != nil {
                    println("Reverse geocoder failed with error" + error.localizedDescription)
                    return
                }
                if placemarks.count > 0 {
                    let pm = placemarks[0] as CLPlacemark


                    println(pm.locality)
                }


                else {
                    println("Problem with the data received from geocoder")
                }

in the logs I get

//-122.0312186
//37.33233141
//C.CLLocationCoordinate2D
//fatal error: unexpectedly found nil while unwrapping an Optional value

It seems that the CLLocationCoordinate2DMakefunction is failing, which then causes the fatal error in the reverseGeocodeLocation function. Have I mucked up the format somewhere?

TimWhiting
  • 2,405
  • 5
  • 21
  • 41
  • You didn't create the CLLocation that has to be passed as the location. The location variable is your case is a CLLocationCoordinate2D meaning that is a co-ordinate. You had to create the CLLocation rather than CLLocationCoordinate2D from the coordinate you created. – Natasha Mar 20 '16 at 09:37

1 Answers1

70

you never reverse geocode the location but you pass in manager.location.

see: CLGeocoder().reverseGeocodeLocation(manager.location, ...

I assume that was a copy&paste mistake and that this is the issue - the code itself looks good - almost ;)

working code

    var longitude :CLLocationDegrees = -122.0312186
    var latitude :CLLocationDegrees = 37.33233141
    
    var location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!
    println(location)
    
    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
        println(location)
        guard error == nil else {
            println("Reverse geocoder failed with error" + error.localizedDescription)
            return
        }
        guard placemarks.count > 0 else {
            println("Problem with the data received from geocoder")
            return
        }
        let pm = placemarks[0] as! CLPlacemark
        println(pm.locality)
    })
Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • I want detailed address like Street Name, City, State, Country. Your post provides only City.. – Jayprakash Dubey Aug 18 '17 at 11:00
  • You can watch the additional attributes here: https://developer.apple.com/documentation/corelocation/clplacemark – Niko Klausnitzer Jun 26 '18 at 13:08
  • This line doesn't make sense " if error != nil ". If error will occur then it will not be nil. – ShadeToD Mar 03 '20 at 09:56
  • Hi, sorry dont follow im afraid. if error != nil { return } is like an early exit, no? – Daij-Djan Mar 03 '20 at 20:28
  • I have changed it to defensive programming for readability. Hope this will help. – BananZ Mar 23 '20 at 03:42
  • 1
    @BananZ overrode edit rejection others all agreed on and accepted it. It is ok and doesnt change anything. -- But your edit shouldnt have been made. It would have been better if you you had wrote 'lets use guard statements to make things clearerer TBH ;) -- anyway thanks. Maybe it helps – Daij-Djan Mar 23 '20 at 16:34