集群 3.0.3

Cluster 3.0.3

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2019年11月
SPM支持 SPM

Lasha Efremidze 维护。



Cluster 3.0.3

  • efremidze

Cluster

Build Status Carthage Compatible Language Version License Platform

Cluster 是一个简单的地图注释集群库。此仓库使用一个高效的方法(QuadTree)将标签聚合到集群中。

Demo Screenshots

特性

  • 添加/移除注释
  • 聚类注释
  • 多个管理者
  • 动态禁用集群
  • 自定义单元格大小
  • 自定义注释视图
  • 动画支持
  • 文档

要求

  • iOS 8.0+
  • Xcode 9.0+
  • Swift 5 (Cluster 3.x), Swift 4 (Cluster 2.x), Swift 3 (Cluster 1.x)

演示

示例是一个很好的入门点。它演示了如何

  • 集成库
  • 添加/移除注释
  • 重新加载注释
  • 配置注释视图
  • 配置管理者

Demo GIF

演示视频

$ pod try Cluster

安装

Cluster 可通过 CocoaPods 和 Carthage 获得。

CocoaPods

要使用 CocoaPods 安装 Cluster,请将以下代码添加到您的 Podfile 文件中

pod "Cluster"

Carthage

要使用 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

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

Communication

  • 如果你 发现了错误,请提交一个问题。
  • 如果你 有功能请求,请提交一个问题。
  • 如果你想 贡献,请提交一个拉取请求。

Mentions

Credits

License

集群采用MIT许可证,更多信息请参阅LICENSE文件。