ios - 注释不会加载到 map 上

标签 ios swift annotations mapkit cloudkit

一段时间以来,我一直在尝试获取 map 上各处注释的方向。我最近发现了如何做到这一点,但现在并不是我所有的注释都会显示在 map 上,只有一个会显示。我在 Cloudkit 中使用公共(public)数据库来存储我所有的用户信息。自从我使变量 annotation = MKPointAnnotation() 可用于整个 View Controller 以来,并不是我所有的注释都显示在 map 上。

这是我获取路线的方式:

let annotation = MKPointAnnotation()

@IBAction func getDirections(_ sender: Any) {
    let view = annotation.coordinate        
    print("Annotation: \(String(describing: view ))")
    let currentLocMapItem = MKMapItem.forCurrentLocation()
    let selectedPlacemark = MKPlacemark(coordinate: view, addressDictionary: nil)
    let selectedMapItem = MKMapItem(placemark: selectedPlacemark)
    let mapItems = [selectedMapItem, currentLocMapItem]
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
    MKMapItem.openMaps(with: mapItems, launchOptions:launchOptions)
}

这是我显示注释的方式:

func fetch() {
    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)

        self.annotation.title = record?["username"] as? String
        self.annotation.subtitle = record?["hours"] as? String
        if let location = record?["location"] as? CLLocation {
            self.annotation.coordinate = location.coordinate
        }

        print("recordFetchedBlock: \(record)")

        self.mapView.addAnnotation(self.annotation)
    }

    queryOperation.queryCompletionBlock = { (cursor, error) in

        print("queryCompletionBlock: \(self.truck)")
    }

    database.add(queryOperation)
}

我现在遇到这个错误:

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes. 

最佳答案

正如@Surjeet 所说,在主线程中编写所有 UI 更改。

DispatchQueue.main.async {
    self.mapView.addAnnotation(self.annotation)
}

关于ios - 注释不会加载到 map 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746492/

相关文章:

iphone - 从 StoryBoard 添加的 UIActivityIndi​​catorView 不显示,除非以编程方式添加为 subview

ios - 按下表格 View 单元格时如何添加深色?

在 Core Data 可转换字符串数组上返回匹配项的 Swift 谓词

java - 你能接受可变参数作为注释中的值吗?

java - 使用注释映射时还需要 XML 映射吗?

groovy - 如果注释包含闭包字段,则使用注释字段实现特征的类会丢失特征字段上的注释

ios - 如何理解superview

ios - 自动定义当前索引

ios - 辞职 iOS 应用程序 "Product identifiers don' t 匹配”

ios - 如何在 Swift 中从 JSON 解析值大于 2147483647 的 Int