2017-03-22 7 views
0

私は更新したいアドレスのリストを持っています。アドレスのリストを配列に読み込んだら、CLGeocoderのgeocodeAddressString関数を呼び出します。CLGeocoderの完了ハンドラからリスト内のアドレスを更新する方法geocodeAddressString()

補完ハンドラでは、アドレスリスト内のどのインデックスがgeocodeAddressString呼び出しの作成に使用されたかをどのように知っていますか?

基本的に私はどのように私は、この例= 1で、私はインデックスを知っていますかどのようにも場所アレイに戻って取得してください

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 

を設定したいと思いますか?

私は配列全体をループすることができればと考えています。

私はあなたの場所がクラスであると仮定し
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

はあなたのアレイの上に行くと、後でこの場所を使用します。

for location in locations { 

//... 
    // in your completion handler 
    location.lat = placemark.location!.coordinate.latitude 
//... 
} 

PS:場所が構造体であるならば、あなたは別の操作を行う必要があります戦略

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 

    } 
}) 
} 
関連する問題