我有一个要更新的地址列表,并添加了经纬度。将地址列表读入数组后,我调用 CLGeocoder 中的 geocodeAddressString 函数。
在完成处理程序中,我如何知道地址列表中的哪个索引用于进行 geocodeAddressString 调用?
基本上我想设置
locations[1].MailAddr1 = placemark.name
locations[1].MailCity = placemark.locality
locations[1].MailState = placemark.administrativeArea
locations[1].MailZip = placemark.postalCode
locations[1].lat = placemark.location!.coordinate.latitude
locations[1].long = placemark.location!.coordinate.longitude
如何返回位置数组以及如何知道索引(在本例中 = 1)?
我希望能够循环整个数组。
let locations = ReadCSV()
let address = locations[1].MailAddr1 + ", " + locations[1].MailCity + ", " + locations[1].MailState + ", " + locations[1].MailZip + ", " + locations[1].MailCountry
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error)
}
if let placemark: CLPlacemark = placemarks?.first {
let name = placemark.name
let address = placemark.thoroughfare
let locality = placemark.locality
let zip = placemark.postalCode
let subLocality = placemark.subLocality
let country = placemark.country
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
let location: CLLocation = placemark.location!
}
})
最佳答案
我假设你的位置是一个类:
检查您的数组,稍后使用此位置:
for location in locations {
//...
// in your completion handler
location.lat = placemark.location!.coordinate.latitude
//...
}
PS:如果位置是一个结构,那么你需要采取不同的策略
关于swift - 如何从 CLGeocoder geocodeAddressString() 中的完成处理程序更新列表中的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42959142/