我是新雨燕。我有一个任务需要完成。我从服务器收到响应,在那里我得到了纬度和经度的数字。
{
EndLat = "28.511593";
EndtLong = "77.071136";
},
{
EndLat = "28.511593";
EndtLong = "77.071136";
},.....
获得这种类型的响应有超过 100 个条目。
现在,当我通过反向地理编码获取地址时。
func getAddressForLogOut(pdblLatitude: String, withLongitude pdblLongitude: String) {
if (pdblLatitude != "") && (pdblLongitude != "") {
var center : CLLocationCoordinate2D = CLLocationCoordinate2D()
let lat: Double = Double("(pdblLatitude)")!
//21.228124
let lon: Double = Double("(pdblLongitude)")!
//72.833770
let ceo: CLGeocoder = CLGeocoder()
center.latitude = lat
center.longitude = lon
let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)
ceo.reverseGeocodeLocation(loc, completionHandler:
{(placemarks, error) in
if (error != nil)
{
print("reverse geodcode fail: (error!.localizedDescription)")
}
let pm = placemarks! as [CLPlacemark]
if pm.count > 0 {
let pm = placemarks![0]
print(pm.country)
print(pm.locality)
print(pm.subLocality)
print(pm.thoroughfare)
print(pm.postalCode)
print(pm.subThoroughfare)
var addressString : String = ""
if pm.subLocality != nil {
addressString = addressString + pm.subLocality! + ", "
}
if pm.thoroughfare != nil {
addressString = addressString + pm.thoroughfare! + ", "
}
if pm.locality != nil {
addressString = addressString + pm.locality! + ", "
}
if pm.country != nil {
addressString = addressString + pm.country! + ", "
}
if pm.postalCode != nil {
addressString = addressString + pm.postalCode! + " "
}
print(addressString)
self.endAdd = addressString
print("self.endAdd is",self.endAdd)
}
} )
}
}
我在tableView的cellForRowAtIndexPath函件中使用该函数。 当我滚动表格视图时,应用程序崩溃。 在地标处为零!
让 pm = 地标!作为 [CLPlacemark] 在此行。
任何人都可以帮助我使用反向地理编码,让我获取 100 多个纬度和经度的地址并将其显示在 tableView 中。
CLGeocoder 返回一个可选类型:
typealias CLGeocodeCompletionHandler = ([CLPlacemark]?, Error?) -> Void
(来自文档(
你应该检查一下。
我认为使用单元格从地理编码器加载数据是一个坏主意,因为您将面临一场比赛:
- 启动请求1(LAT1,L1(
- 重复使用单元格
- 启动请求2(LAT2,LON2(
- 地理编码器对请求 1 的响应
在 4( 时,单元格显示不一致的数据。