带集群的MapView:如何在同一视图上显示多个注释 [英] MapView with clusters: how to display multiple annotations on same view
问题描述
我在mkmapview
上集成了 FBAnnotationClustering ,效果很好,但是当存在多个在完全相同的位置添加注释,我无法显示带有正确信息的注释视图.我试图遍历注释并在同一视图中显示它们(每个注释都带有标注按钮以显示更多信息).
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?
这是我的集群班级:
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
}
}
推荐答案
是的,如果您的点位置具有相同的纬度-经度,那么您将无法单独显示所有点. 您可以尝试破解. 您可以分离所有具有相同纬度-经度的点的列表,并在将它们应用于聚类之前,只需将其经纬度值之一更改为+或-0.00007并创建一个模式. 显示不会有太大区别,您可以在群集中显示所有点.
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.
这篇关于带集群的MapView:如何在同一视图上显示多个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!