ios - 带有集群 : how to display multiple annotations on same view 的 MapView

标签 ios swift mkmapview mkannotation mkannotationview

我已经整合了FBAnnotationClusteringmkmapview 上运行完美,但是当在完全相同的位置有多个注释时,我无法显示带有正确信息的 annotationView。我正在尝试遍历注释并将它们显示在同一 View 中(每个注释都有一个标注按钮以显示更多信息)。

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

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

最佳答案

是的,如果您的点位置具有相同的纬度 - 经度,那么您将无法单独显示所有点。 你可以试试hack。 您可以单独列出所有具有相同纬度 - 经度的点,在将它们应用于聚类之前,只需将其中一个经纬度值更改为 + 或 - 0.00007 并创建一个模式。 它在显示上不会有太大区别,您可以将所有点显示在集群中。

关于ios - 带有集群 : how to display multiple annotations on same view 的 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31537450/

相关文章:

ios - ld : library not found for -lVuforia

iphone - 在 map 上放置注释

swift - 如何在移动 map 时更改注释的大小?

ios - 确定 map View 的当前缩放比例

ios - SpriteKit 中的运动轨迹效果?

swift - 无法在 Xcode Playgrounds 中创建文件夹?

ios - 如何在 Swift 中将小数截断到 x 位

ios - 保存 map 注释

ios - 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行

ios - 更改 UIControlState.Highlighted 上的 UIButton backgroundColor?