Erva Erva - 1 year ago 112
iOS Question

Coordinates to MKPointAnnotation from CloudKit

I have coordinates in CloudKit as Location data type ("Koordinaatit"). I'am trying to draw MKPointAnnotation to map from these coordinates but i don't know how to get coordinates from array to annotation.

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'

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 {

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


override func viewDidLoad() {


let sijainnitLkm = sijainnitArray.count - 1

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


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


Answer Source

You want to use the method object(forKey:) on your CKRecord. Presumably the key you want to use is "Koordinaatit".

I haven't used CloudKit before. It looks like you can save a CLLocation directly into a CKRecord.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download