MapView with clusters: how to display multiple ann

2019-05-21 04:50发布

问题:

I have integrated FBAnnotationClustering on mkmapview which works perfectly, but when there is more than one annotation at exactly the same place I cannot display the annotationView with the correct informations. I'm trying to loop through the annotations and display them in the same view (each with a callout button to display more info).

So far I managed to make one callout work to display the details at the bottom, but in the annotationView it displays it without the right title, and there is just one so it's not exactly useful. So I'm wondering, is it possible to display multiple annotations on the same view? Any idea how I could achieve this?

Here is my cluster class:

class FBAnnotationCluster : NSObject {

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

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

My single annotation class:

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

and the 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:

Yes, If your point location having same latitude - longitude then you will not be able to display all points separately. You can try on hack. You can separate list of all those points having same latitude - longitude and before applying them for clustering, just change one of it's lat-long value to + or - 0.00007 and create one pattern. It will not make big difference in displaying and you can display all you points in cluster.