Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Geocode Location in Swift [closed]

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?

like image 500
TimWhiting Avatar asked Dec 15 '14 23:12

TimWhiting


People also ask

How do you reverse a geocode in Swift?

Reverse Geocoding With Swift. Reverse geocoding is a simple concept. We hand the CLGeocoder class a set of coordinates, latitude and longitude, and ask it for the corresponding address, a physical location that has meaning to the user.

Is reverse geocoding free?

Reverse Geocoding and Geolocation Service by Noggle is a free API that allows developers to embed the functionality to locate the largest city or nearest one to the latitude to longitude location.

How get lat long from address in iOS Swift?

geocodeAddressString(addressString) { (placemarks, error) in if error == nil { if let placemark = placemarks?[0] { let location = placemark. location! completionHandler(location. coordinate, nil) return } } completionHandler(kCLLocationCoordinate2DInvalid, error as NSError?) } }

How does reverse geocoding work?

Reverse geocoding identifies locations nearest to a latitude and longitude coordinate, enabling increased reliability of the location data. Even when addresses are unavailable, reverse geocoding software can provide corresponding addresses, based on nearby landmarks, for geolocation queries.


1 Answers

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)     }) 
like image 167
Daij-Djan Avatar answered Sep 25 '22 00:09

Daij-Djan