【问题标题】:Showing a CKRecord on the map using its "Location"使用其“位置”在地图上显示 CKRecord
【发布时间】:2017-12-04 04:33:31
【问题描述】:

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

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()
}

【问题讨论】:

    标签: ios swift mapkit cloudkit


    【解决方案1】:

    您的CKRecord 将有一个位置字段。这将是CLLocation 类型的值。

    MKPointAnnotationcoordinate 属性是 CLLocationCoordinate2D 类型。

    CLLocation 有一个coordinate 属性,它也是CLLocationCoordinate2D 类型。

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

    【讨论】:

    • 感谢 rmaddy 的回答!我插入了答案并运行了我的应用程序,它不再崩溃了哈哈。它仍然没有显示我想要的内容,但我可能在插入您的答案时出错了。我将我的问题编辑为目前的样子.....
    • 我回滚了您的编辑,因为您的编辑使我的答案过时了。使用调试器。在新的if 语句上放置一个断点。验证它是否符合您的预期。并查看我的更新答案。我有一个错字。
    • 你真是个天才!甚至不必使用断点,只需复制并粘贴答案。再次感谢
    猜你喜欢
    • 2015-05-16
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多