带标注的ios mapkit注释群集

带标注的ios mapkit注释群集,ios,swift,ios11,markerclusterer,callout,Ios,Swift,Ios11,Markerclusterer,Callout,目前,我正在尝试使用iOS 11中引入的新群集功能。然而,我发现当我使用集群功能时,我失去了初始注释的功能,包括标注和单击注释时发生的事情。我相信这是因为我在不同于注释的类中创建集群。似乎在我当前的代码中,我只能有一个或另一个。缩小时不群集的自定义管脚,或使用丢失的信息群集管脚。有人知道更好的方法吗?下面我附上了代码,展示了如何创建插针标注和如何创建群集。有没有一种方法可以同时具备这两种功能 创建详图索引 func mapView(_ mapView: MKMapView, viewFor an

目前,我正在尝试使用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)
            }
        }
    }
}