2016-11-27 5 views
0

CloudKitに座標データ型( "Koordinaatit")があります。私はこれらの座標からマップするMKPointAnnotationを描画しようとしているが、私はどのように配列から注釈に座標を取得するのか分からない。CloudKitからのMKPointAnnotationの座標

var sijainnitArray:[CKRecord] = [] 


    func drawPin(sijainti: Int) { 

     let annotation = MKPointAnnotation() 
     annotation.coordinate = CLLocationCoordinate2D(sijainnitArray[sijainti].Koordinaatit) //XCode shows error: Value of type 'CKRecord' has no memeber 'Koordinaatit' 
     self.mapView.addAnnotation(annotation) 
} 


func fetchRecordsFromCloud() { 

     let cloudContainer = CKContainer.default() 
     let publicDatabase = cloudContainer.publicCloudDatabase 
     let predicate = NSPredicate(value: true) 
     let query = CKQuery(recordType: "Sijainti", predicate: predicate) 

     publicDatabase.perform(query, inZoneWith: nil, completionHandler: { 
      (results, error) -> Void in 

      if error != nil { 
       print(error) 
       return 
      } 

      if let results = results { 
       print("Completed the download of locations data") 
       self.sijainnitArray = results 
       } 
     }) 

    } 



override func viewDidLoad() { 
    super.viewDidLoad() 

    fetchRecordsFromCloud() 

    let sijainnitLkm = sijainnitArray.count - 1 

    for i in 0...sijainnitLkm { 
     drawPin(sijainti: i) 
     } 
} 



**ANSWER** 

let sijaintilista = self.sijainnitArray[i] 

      let sijaintiLatLong = sijaintilista.object(forKey: "Koordinaatit") as? CLLocation 
      let sijaintiLat = sijaintiLatLong?.coordinate.latitude 
      let sijaintiLon = sijaintiLatLong?.coordinate.longitude 

      let sourceLocation = CLLocationCoordinate2D(latitude: sijaintiLat!, longitude: sijaintiLon!) 
      let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil) 

      if let location = sourcePlacemark.location { 
       annotation.coordinate = location.coordinate 
      } 

      self.mapView.addAnnotation(annotation) 

答えて

0

あなたCKRecord上の方法object(forKey:)を使用します。おそらくあなたが使用したいキーは "Koordinaatit"です。

私は以前CloudKitを使用していません。 CLLocationを直接CKRecordに保存できるようです。

+0

locationManagerの場所がCLLocationとしてCloudKit CKRecordに保存されていますが、CLLocationをMKPointAnnotationに追加する方法がわかりません。 let location = locations.last! publicDatabase = CKContainer.default()publicCloudDatabase はせましょうレコード= CKRecord(RECORDTYPE: "Sijainti")。 record.setObject(場所、forKey: "Koordinaatit") publicDatabase.save(レコード、completionHandler:{ (record、error) - > Void in – Erva

+0

あなたは 'object(forKey:)'を使ってcloudKitからレコードを読み込み、それを 'CLLocation'にキャストします。 –

+0

私の答えに追加された解決策。 – Erva

関連する問題