Cluster 是一个简单的地图标注聚类库。 此仓库使用一种高效的方法 (QuadTree) 将图钉聚合到一个集群中。
示例 是一个很好的入门地方。 它演示了如何
$ pod try Cluster
Cluster 可通过 CocoaPods 和 Carthage 获得。
要使用 CocoaPods 安装 Cluster,请将以下内容添加到您的 Podfile
pod "Cluster"
要使用 Carthage 安装 Cluster,请将以下内容添加到您的 Cartfile
github "efremidze/Cluster"
ClusterManager 类生成、管理和显示标注集群。
let clusterManager = ClusterManager()
创建一个符合 MKAnnotation 协议的对象,或扩展一个现有的对象。 接下来,使用 add(annotation:) 将标注对象添加到 ClusterManager 的一个实例中。
let annotation = Annotation(coordinate: CLLocationCoordinate2D(latitude: 21.283921, longitude: -157.831661))
manager.add(annotation)
实现地图视图的 mapView(_:viewFor:) 委托方法来配置标注视图。 返回一个 MKAnnotationView 的实例以显示标注的可视化表示。
要显示集群,返回一个 ClusterAnnotationView 的实例。
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? ClusterAnnotation {
return CountClusterAnnotationView(annotation: annotation, reuseIdentifier: "cluster")
} else {
return MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
}
}
}
出于性能原因,您通常应该在地图视图中重用 MKAnnotationView 对象。 请参阅示例了解更多信息。
ClusterAnnotationView 类公开了一个 countLabel 属性。 您可以子类化 ClusterAnnotationView 以根据需要提供自定义行为。 这是一个子类化 ClusterAnnotationView 并自定义图层 borderColor 的示例。
class CountClusterAnnotationView: ClusterAnnotationView {
override func configure() {
super.configure()
self.layer.cornerRadius = self.frame.width / 2
self.layer.masksToBounds = true
self.layer.borderColor = UIColor.white.cgColor
self.layer.borderWidth = 1.5
}
}
请参阅AnnotationView 了解更多信息。
您可以通过设置标注的 style 属性来定制 StyledClusterAnnotationView 的外观。
let annotation = Annotation(coordinate: CLLocationCoordinate2D(latitude: 21.283921, longitude: -157.831661))
annotation.style = .color(color, radius: 25)
manager.add(annotation)
在 ClusterAnnotationStyle 枚举中提供了几种样式
color(UIColor, radius: CGFloat) - 将标注显示为一个圆圈。image(UIImage?) - 将标注显示为图像。添加标注后,您需要返回 StyledClusterAnnotationView 的一个实例来显示带样式的标注。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? ClusterAnnotation {
return StyledClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, style: style)
}
}
要删除标注,您可以调用 remove(annotation:)。 但是,标注仍将显示,直到您调用 reload()。
manager.remove(annotation)
如果 shouldRemoveInvisibleAnnotations 设置为 false,则已删除的标注可能仍会显示在地图上,直到在可见区域上调用 reload()。
实现地图视图的 mapView(_:regionDidChangeAnimated:) 委托方法,以便在区域更改时重新加载 ClusterManager。
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
clusterManager.reload(mapView: mapView) { finished in
// handle completion
}
}
每当您添加或删除标注时,都应调用 reload()。
ClusterManager 类公开了几个属性来配置聚类
var zoomLevel: Double // The current zoom level of the visible map region.
var maxZoomLevel: Double // The maximum zoom level before disabling clustering.
var minCountForClustering: Int // The minimum number of annotations for a cluster. The default is `2`.
var shouldRemoveInvisibleAnnotations: Bool // Whether to remove invisible annotations. The default is `true`.
var shouldDistributeAnnotationsOnSameCoordinate: Bool // Whether to arrange annotations in a circle if they have the same coordinate. The default is `true`.
var distanceFromContestedLocation: Double // The distance in meters from contested location when the annotations have the same coordinate. The default is `3`.
var clusterPosition: ClusterPosition // The position of the cluster annotation. The default is `.nearCenter`.
ClusterManagerDelegate 协议提供了许多函数来管理聚类和配置单元格。
// The size of each cell on the grid at a given zoom level.
func cellSize(for zoomLevel: Double) -> Double? { ... }
// Whether to cluster the given annotation.
func shouldClusterAnnotation(_ annotation: MKAnnotation) -> Bool { ... }
Cluster 在 MIT 许可下可用。 有关更多信息,请参见 LICENSE 文件。