使用 "Location" 在地图上显示 CKRecord

Showing a CKRecord on the map using its "Location"

我正在尝试让自定义区域中的每个 CKRecord 作为注释单独显示在我的地图上。我将注释标题和副标题用作 CKRecord 中的其他字段,但对于 annotation.cordinate,我很难将其连接到 CKRecord 的 "location" 字段。

func fetchTruck() {
    let truePredicate = NSPredicate(value: true)
    let eventQuery = CKQuery(recordType: "User", predicate: truePredicate)
    let queryOperation = CKQueryOperation(query: eventQuery)

    queryOperation.recordFetchedBlock = { (record : CKRecord!) in
        self.truck.append(record)
        let annotation = MKPointAnnotation()
        annotation.title = record["username"] as? String
        annotation.subtitle = record["hours"] as? String
        **if let location = record["location"] as? CLLocation {
annotation.coordinate = location.coordinate}** Answer 

        print("recordFetchedBlock: \(record)")

        self.mapView.addAnnotation(annotation)
    }

    queryOperation.queryCompletionBlock = { (cursor, error) in
        print("queryCompletionBlock: \(self.truck)")
    }

    database.add(queryOperation)
}

override func viewDidLoad() {
    super.viewDidLoad()

    fetchTruck()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

您的 CKRecord 将有一个位置字段。那将是 CLLocation.

类型的值

MKPointAnnotationcoordinate属性是CLLocationCoordinate2D类型。

CLLocation 有一个 coordinate 属性 也是 CLLocationCoordinate2D.

类型
if let location = record["location"] as? CLLocation {
    annotation.coordinate = location.coordinate
}