从 CloudKit 到 MKPointAnnotation 的坐标

Coordinates to MKPointAnnotation from CloudKit

我在 CloudKit 中有坐标作为位置数据类型 ("Koordinaatit")。我正在尝试绘制 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)

您想在 CKRecord 上使用方法 object(forKey:)。大概您要使用的密钥是 "Koordinaatit".

我以前没用过CloudKit。看起来您可以将 CLLocation 直接保存到 CKRecord.