[英]How to update address in list from completion handler in CLGeocoder geocodeAddressString()
我有一個要更新的地址列表,也要添加緯度。 將地址列表讀入數組后,我在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:如果位置是一個結構,那么您需要采取其他策略
嘗試使函數並發送每個位置作為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
}
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.