swift - 如何从 CLGeocoder geocodeAddressString() 中的完成处理程序更新列表中的地址

标签 swift swift3 geolocation

我有一个要更新的地址列表,并添加了经纬度。将地址列表读入数组后,我调用 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/

相关文章:

ios - 如何构建仍然兼容存档并上传到 AppStore 的 Framework fat 二进制文件(所有 archs)?

swift - 在函数中传递一个闭包

objective-c - ReactiveCocoa 5.0 中的 RACObserve(object, keyPath)

ios - swift 3 : Understand syntax change for UITableViewDataSource method

ios - 如何在 swift 3 中制作单选按钮

ios - 连接到 XCode 后,iPhone 位置服务显示错误的位置

xcode - XCode:调试=>“模拟位置”菜单项不可用(灰色)

ios - 如何在TableController的ImageView中正确添加图片?

ios - XCode 自动完成

javascript - 如何在 Google map 中结合地理位置和海拔