I have a latitude/longitude location that I want to convert to the location name String in Swift. What is the best way to do this? i believe it's best to use the reverseGeocodeLocation function but not exactly sure how to. This is what I have so far:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if (locationFixAchieved == false) {
locationFixAchieved = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
var coord = locationObj.coordinate
var latitude = coord.latitude
var longitude = coord.longitude
getCurrentWeatherData("\(longitude)", latitude: "\(latitude)")
reverseGeocodeLocation(location:coord, completionHandler: { (<#[AnyObject]!#>, <#NSError!#>) -> Void in
<#code#>
})
}
}
func reverseGeocodeLocation(location: CLLocation!, completionHandler: CLGeocodeCompletionHandler!){
}
Swift Code to get address of the placemark, nicely Formatted
please check this answer.
You need to write the code like this:
I am using Swift 3 / XCode 8
I used Benjamin Herzog's answer but I ran into some build errors related to optional and casting.
In rewriting it, I decided to encapsulate it in a function and generalize it so it can easily be plugged in anywhere.
Using it: