带标注的ios mapkit注释聚类

时间:2018-01-19 15:08:03

标签: ios swift ios11 markerclusterer callout

目前我正在尝试使用iOS 11中引入的新群集功能。但是我发现当我使用群集功能时,我会丢失初始注释的功能,包括标注以及单击注释时会发生什么。我相信这是因为我在与注释不同的类中创建了集群。好像用我现在的代码,我只能有一个或另一个。缩小时不会群集的自定义引脚,或者丢失信息的群集。有谁知道更好的方法吗?下面我附上了代码,显示了如何创建引脚标注以及如何创建集群。有没有办法可以同时拥有这两种功能?

创建标注

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if #available(iOS 11.0, *) {
    // 2
    guard let annotation = annotation as? CalloutPin else { return nil }
    // 3
    let identifier = "Pin"
    var view: MKMarkerAnnotationView
    // 4
    if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        as? MKMarkerAnnotationView {
        dequeuedView.annotation = annotation
        view = dequeuedView
    } else {
        // 5
        view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        view.canShowCallout = true
        view.calloutOffset = CGPoint(x: -5, y: 5)
        view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }
        return view
    }
    else{
        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin-annotation")

        annotationView.animatesDrop = true
        annotationView.canShowCallout = true
        return nil
    }

}

创建群集的代码

@available(iOS 11.0, *)
class PinView :MKMarkerAnnotationView {
     override var annotation: MKAnnotation? {
        willSet {
          //if let pin = newValue as? Pin {
           clusteringIdentifier = "pin"
           displayPriority = .defaultHigh

           //}
         }
     }
 }

@available(iOS 11.0, *)
class ClusterView: MKAnnotationView {

   override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
    displayPriority = .defaultHigh
    collisionMode = .circle
    centerOffset = CGPoint(x: 0, y: -10) // Offset center point to animate better with marker annotations
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override var annotation: MKAnnotation? {
    willSet {
        if let cluster = newValue as? MKClusterAnnotation {
            let renderer = UIGraphicsImageRenderer(size: CGSize(width: 40, height: 40))
            let count = cluster.memberAnnotations.count

            image = renderer.image { _ in
                // Fill full circle with tricycle color
                UIColor.red.setFill()
                UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40)).fill()

                // Finally draw count text vertically and horizontally centered
                let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.white,
                                   NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 20)]
                let text = "\(count)"
                let size = text.size(withAttributes: attributes)
                let rect = CGRect(x: 20 - size.width / 2, y: 20 - size.height / 2, width: size.width, height: size.height)
                text.draw(in: rect, withAttributes: attributes)
            }
        }
    }
}

0 个答案:

没有答案