swift - MapKit 聚类在开始时没有正确聚类,它在平移后聚类
问题描述
我在 MapKit 中的集群有问题,它在开始时无法正常工作,我必须平移一些时间来强制 MapKit “重新计算”集群。下面的视频显示了这个问题。
我的代码说明
我有一个class MapKitViewController: UIViewController, MKMapViewDelegate
方法类
override func loadView() {
super.loadView()
mapView = MKMapView(frame: view.bounds)
mapView.delegate = self
view.addSubview(mapView)
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
mapView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
private func registerAnnotationViewClasses() {
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(ClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
}
override func viewDidLoad() {
super.viewDidLoad()
registerAnnotationViewClasses()
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MyAnnotation {
let v = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
v.titleVisibility = .hidden
v.clusteringIdentifier = "clusterID"
return v
} else {
return nil
}
}
这是我的集群注释视图类
class ClusterAnnotationView: MKAnnotationView {
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
setUp()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}
private func setUp() {
collisionMode = .circle
}
override func prepareForDisplay() {
super.prepareForDisplay()
displayPriority = .defaultHigh
if let clusterAnnotation = annotation as? MKClusterAnnotation {
image = image(forMembersCount: clusterAnnotation.memberAnnotations.count)
}
}
private func image(forMembersCount membersCount: Int) -> UIImage {
let size = /* calculate size */
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image { _ in
/* draw an image */
}
}
}
我能做些什么来解决这个问题?
解决方案
在
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MyAnnotation {
let v = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
v.titleVisibility = .hidden
v.clusteringIdentifier = "clusterID"
return v
} else {
return nil
}
}
您正在重新创建所有 annotationViews,可能每次平移时。
这些旨在重复使用,而不是每次都重新创建。所以 mapView 认为您正在用新对象替换您的 annotationViews(这就是您实际在做的事情)。这意味着现有视图不能平滑过渡 - 旧视图不再存在。
请参阅标题“配置注释视图”后的https://www.raywenderlich.com/7738344-mapkit-tutorial-getting-started#toc-anchor-009 ,了解如何重用注释视图。
推荐阅读
- gupshup - bot-script goto 在主要部分不起作用
- android - 如何在发布模式下检索 Firebase 存储图像?
- ruby-on-rails - 'SyntaxError 语法错误,意外的 tIDENTIFIER,期望 kEND)的验证未通过:'
- recursion - 我的消息传递示例有什么问题?
- database - 设计摘要电子邮件实施的方法?
- javascript - navCtrl ionic 3 无效链接
- python - 通过替换熊猫中单元格中的第二行将值从一列连接到另一列
- java - 如何在 JavaFX 中动态更新 ListView 单元格?
- django - django admin 条件关系
- php - 使用 sql 过滤最新评论