只是看起来像这样:
显示超过2k个标签。当我加载该控制器时,完成显示需要10秒以上。为什么? 如何加快速度?
这就是我在地图集上显示它们的方法:
private func setupMapBox() {
let url = URL(string: "mapbox://styles/kunass2/cjcubgbvq1uuq2smt3nup7nzf")
mapView = MGLMapView(frame: .zero, styleURL: url)
mapView.delegate = self
mapContainerView.addSubview(mapView)
mapView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
private func setupFetchedResultsController() {
let context = NSManagedObjectContext.mr_default()
let fetchRequest = NSFetchRequest<Label>(entityName: "Label")
let nameDescriptor = NSSortDescriptor(key: "text", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
fetchRequest.predicate = NSPredicate(format: "territory.identifier = %@", territory.identifier)
fetchRequest.sortDescriptors = [nameDescriptor]
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
try! fetchedResultsController.performFetch()
reloadAnnotations()
}
private func reloadAnnotations() {
mapView.removeAnnotations(mapView.annotations ?? [])
mapView.addAnnotations(fetchedResultsController.fetchedObjects!)
}
以下是我的代表:
//MARK: - NSFetchedResultsControllerDelegate
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
if let label = anObject as? Label {
switch type {
case .insert:
mapView.addAnnotation(label)
case .delete:
mapView.removeAnnotation(label)
case .update:
fallthrough
case .move:
mapView.removeAnnotation(label)
mapView.addAnnotation(label)
}
}
}
//MARK: - MGLMapViewDelegate
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
setupFetchedResultsController()
}
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
guard let label = annotation as? Label else {
return nil
}
return LabelAnnotationView(label: label)
}
在viewDidLoad
中的我只是致电:
setupMapBox()
答案 0 :(得分:1)
MGLSymbolStyleLayer应该更高效。 This example包含使用文本的符号图层。因为符号图层直接在GL中渲染,所以它们更轻量级。
注释使用本机UIViews,因此在使用多个时效果较差。有关向地图添加点的不同方法的优缺点的详细信息,请参阅this guide。