【问题标题】:Map annotations overlap when they should get clustered地图注释在应该聚集时重叠
【发布时间】:2018-12-31 14:41:57
【问题描述】:

我正在开发一个 iOS swift 应用程序,该应用程序将图像显示为地图上的注释。当多个注释重叠时,它们应该聚集在一起,而不是显示为一张图像。

Apple 提供的集群有时不起作用。缩小并多次放大时,图像可能会重叠很多。

它应该是这样的:

这是发生错误时的样子:

我已将示例视频上传到 Youtube (https://youtu.be/kaI0bTS8_HY)

关于实现的一些细节:

  • 我正在使用专门的 MKAnnotationView:

    class PhotoAnnotationView: MKAnnotationView {
      let annotationWidth = 100
    
      var imageView = UIImageView()
    
      override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        clusteringIdentifier = "LocatedPhoto"
        collisionMode = .circle
    
        addSubview(imageView)
        imageView.frame = annotationFrame
        frame = annotationFrame
      }
    
      var annotationFrame: CGRect {
        return CGRect(x: 0, y: 0, width: annotationWidth, height: annotationWidth)
      }
    
      override var annotation: MKAnnotation? {
        willSet {
            if let annotation = newValue as? PhotoAnnotation {
                let url = annotation.locatedPhoto.thumbnailURL
                let displayLocation = annotation.displayLocation
                setImage(url: url)
            }
        }
      }
    
      func setImage(url: URL) {
        //...
      } 
    }
    

MapViewController 注册了这个注解类型来显示注解:

mapView.register(PhotoAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

省略,因为我认为这不是问题的根源: - MKClusterView(常规 ClusterView 也存在错误) - 在地图视图中添加注释

这个问题的根源是什么?

【问题讨论】:

    标签: ios swift mapkit mkannotation mkannotationview


    【解决方案1】:

    PhotoAnnotationView,你的annotation观察者应该重置clusteringIdentifier

    override var annotation: MKAnnotation? {
        willSet {
            if let annotation = newValue as? PhotoAnnotation {
                clusteringIdentifier = "LocatedPhoto"            // make sure to reset this
                let url = annotation.locatedPhoto.thumbnailURL
                let displayLocation = annotation.displayLocation
                setImage(url: url)
            }
        }
    }
    

    随着它们的出队和重用,clusteringIdentifier 可以被清除。通过再次设置它,您可以确保集群仍然会发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多