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

时间:2015-07-21 11:12:20

标签: ios swift mkmapview mkannotation mkannotationview

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

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

map with single annotation map with cluster view

这是我的集群类:

class FBAnnotationCluster : NSObject {

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

var title:String = "cluster"
var subtitle:String? = nil
var annotations:[FBAnnotation] = []  
}

我的单注释类:

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
}   
}

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
    }
}

1 个答案:

答案 0 :(得分:6)

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