我有一个我想要更新的地址列表,还添加了lat long。在将地址列表读入数组后,我在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!
}
})
答案 0 :(得分:0)
我假设你的位置是一个班级:
查看您的数组,然后使用此位置:
for location in locations {
//...
// in your completion handler
location.lat = placemark.location!.coordinate.latitude
//...
}
PS:如果location是一个结构,那么你需要做一个不同的策略
答案 1 :(得分:0)
尝试创建函数并将每个位置发送为inout参数
示例:
for location in locations {
setAddressOflocation(location)
}
func getAddressOflocation (locationObject : inout CLLocation) {
let address = locationObject.MailAddr1 + ", " + locationObject.MailCity + ", " + locationObject.MailState + ", " + locationObject.MailZip + ", " + locationObject.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!
locationObject.lat = placemark.location!.coordinate.latitude
locationObject.long = placemark.location!.coordinate.longitude
}
})
}