DCD*_*CDC 2 reverse-geocoding cllocation ios swift swift3
这是我的代码
if loc.latitude != 0.0 && loc.longitude != 0.0 {
let loca = CLLocation(latitude: loc.latitude, longitude: loc.longitude)
geoCoder.reverseGeocodeLocation(loca) { (placemarks, error) in // this is the last line that is being called
var placemark : CLPlacemark!
placemark = placemarks?[0]
city = (placemark.addressDictionary?["City"] as! String)
}
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中执行此代码段是正确的,没有发生运行时错误.
但是,被调用的最后一行是
geoCoder.reverseGeocodeLocation(loca){(placemarks, error)
我也仔细检查过那loca不是零.
为什么没有调用完成处理程序?
completionHandler在闭包中使用.
检查以下示例:
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// Address dictionary
print(placeMark.addressDictionary, terminator: "")
// Location name
if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
print(locationName, terminator: "")
}
// Street address
if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
print(street, terminator: "")
}
// City
if let city = placeMark.addressDictionary!["City"] as? NSString {
print(city, terminator: "")
}
// Zip code
if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
print(zip, terminator: "")
}
// Country
if let country = placeMark.addressDictionary!["Country"] as? NSString {
print(country, terminator: "")
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5349 次 |
| 最近记录: |