【发布时间】:2020-09-24 09:28:22
【问题描述】:
我正在使用CLGeoCoder 实例通过其方法reverseGeocodeLocation() 获取给定坐标集(纬度和经度)的城市和国家/地区。
文档指定此数据应位于 CLPlacemark 的 country 和 locality 属性中,但结果似乎仅位于自 iOS 11 以来已弃用的 addressDictionary 属性中。
Image of debugging instance showing addressDictionary present but neither country nor locality
因此这有效,但在addressDictionary 的所有三种用法中显示此警告:
'addressDictionary' was deprecated in iOS 11.0: Use @properties
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location, preferredLocale: nil) { (placemarksArray, error) in
if let error = error {
print(error)
} else {
if let placemark = placemarksArray?.last {
var city = ""
if let ct = placemark.addressDictionary?["City"] {
city = "\(ct), "
}
var state = ""
if let st = placemark.addressDictionary?["State"] {
state = "\(st), "
}
var country = ""
if let cot = placemark.addressDictionary?["Country"] {
country = "\(cot)"
}
self.cityCountry = "\(city)\(state)\(country)"
}
}
}
我尝试引用属性,但它们只返回 nil 值。
是我调用了错误的方法还是这是一个错误?
【问题讨论】:
-
stackoverflow.com/questions/47987473/…。希望这会有所帮助!
标签: ios swift clgeocoder clplacemark