带集群的MapView:如何在同一视图上显示多个注释

Kal*_*ney 2 mkmapview mkannotation mkannotationview ios swift

我已经集成了FBAnnotationClustering,mkmapview它完美地工作,但是当在完全相同的位置有多个注释时,我无法使用正确的信息显示annotationView.我正在尝试循环注释并在同一视图中显示它们(每个都带有一个标注按钮以显示更多信息).

到目前为止,我设法让一个标注工作在底部显示细节,但在annotationView中它显示没有正确的标题,并且只有一个因此它不是很有用.所以我想知道,是否可以在同一视图上显示多个注释?知道我怎么能做到这一点?

带有单个注释的地图 使用群集视图映射

这是我的集群类:

class FBAnnotationCluster : NSObject {

var coordinate = CLLocationCoordinate2D(latitude: 39.208407, longitude: -76.799555)

var title:String = "cluster"
var subtitle:String? = nil
var annotations:[FBAnnotation] = []  
}
Run Code Online (Sandbox Code Playgroud)

我的单注释类:

class FBAnnotation : NSObject {

var coordinate: CLLocationCoordinate2D
var id: Int
var title: String
var subtitle: String
var distance: String

init(id: Int, title: String, subtitle: String, distance: String, coordinate: CLLocationCoordinate2D){
    self.id = id
    self.title = title
    self.subtitle = subtitle
    self.distance = distance
    self.coordinate = coordinate
}   
}
Run Code Online (Sandbox Code Playgroud)

viewForAnnotation

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    var reuseId = ""
    if annotation.isKindOfClass(FBAnnotationCluster) {
        reuseId = "Cluster"
        var clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId)
        if mapView.zoomLevel() >= 15 {
            println("Display annotation list")
            let a = annotation as! FBAnnotationCluster
            if a.annotations.count > 1 {
                for annotation in a.annotations {
                    println(annotation.title)
                    clusterView!.enabled = true
                    clusterView!.canShowCallout = true
                    //we add an info button to display the detailed view
                    clusterView!.calloutOffset = CGPoint(x: -5, y: 5)
                    clusterView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
                    clusterView!.tag = annotation.id
                }
            }

        }
        return clusterView
    } else {
        reuseId = "Pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.enabled = true
        pinView!.canShowCallout = true
        //we add an info button to display the detailed view
        pinView!.calloutOffset = CGPoint(x: -5, y: 5)
        pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
        pinView!.image = UIImage(named: "map-annotation")

        let a = annotation as! FBAnnotation
        pinView!.tag = a.id

        return pinView
    }
}
Run Code Online (Sandbox Code Playgroud)

Mon*_*ish 6

是的,如果您的点位置具有相同的纬度 - 经度,那么您将无法单独显示所有点.你可以尝试黑客攻击.您可以将具有相同纬度 - 经度的所有点的列表分开,然后在应用它们进行聚类之前,只需将其中一个lat-long值更改为+或 - 0.00007并创建一个模式.它不会对显示产生很大影响,您可以在群集中显示所有点.