实现这一目标的最佳方法是什么?我有一个函数可以获取给定经度/纬度的 reverseGeocode,它工作得很好。但是因为它在获得地址信息时是异步的,所以我已经执行了将地址提交到数据库的行。我需要完成完成处理程序还是有其他方法。我确实尝试了一个 do/while 循环(不要笑),但没有奏效。
func reverseGeocode() -> Bool {
let geoCoder = CLGeocoder()
let newLocation = CLLocation(latitude: valueLatitude, longitude: valueLongitude)
geoCoder.reverseGeocodeLocation(newLocation, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) in
if error != nil {
println("LocationsMenu - Geocode failed with error: \(error.localizedDescription)")
}
if placemarks.count > 0 {
let myplacemark = placemarks[0] as! CLPlacemark
let addressDictionary = myplacemark.addressDictionary
let address = addressDictionary[kABPersonAddressStreetKey] as! NSString
let city = addressDictionary[kABPersonAddressCityKey] as! NSString
let state = addressDictionary[kABPersonAddressStateKey] as! NSString
let postcode = addressDictionary[kABPersonAddressZIPKey] as! NSString
let country = addressDictionary[kABPersonAddressCountryKey] as! NSString
println("\(address) \(city) \(state) \(postcode) \(country)")
}
self.showMap(placemarks[0] as! CLPlacemark)
})
}