注释不会在地图上加载
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了注释不会在地图上加载相关的知识,希望对你有一定的参考价值。
一段时间以来,我一直试图在地图上找到注释的方向。我最近发现了如何做到这一点,但现在并非我的所有注释都会出现在地图上,只有一个注释会出现。我在Cloudkit
中使用公共数据库来存储我的所有用户信息。自从我为整个视图控制器提供变量annotation = MKPointAnnotation()
以来,我的所有注释都没有在地图上显示。
这就是我获取方向的方式:
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)
}
以上是关于注释不会在地图上加载的主要内容,如果未能解决你的问题,请参考以下文章
在 iPhone 中,我如何知道在使用正向地理编码时我的所有地图注释何时加载?