从CloudKit坐标到MKPointAnnotation

时间:2016-11-27 00:15:30

标签: ios mapkit cloudkit cllocation mkpointannotation

我在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)

1 个答案:

答案 0 :(得分:-1)

您想在object(forKey:)上使用CKRecord方法。据推测,你想要使用的关键是" Koordinaatit"。

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