Code Monkey home page Code Monkey logo

cluster's Introduction

Cluster

Language Version License Platform Carthage compatible

Cluster is an easy map annotation clustering library. This repository uses an efficient method (QuadTree) to aggregate pins into a cluster.

You may want to see the Example first if you'd like to see the actual code.

$ pod try Cluster

Features

  • Adding/Removing Annotations
  • Clustering Annotations
  • Multiple Managers
  • Dynamic Cluster Disabling
  • Custom Cell Size
  • Custom Annotation Views
  • Animation Support

Requirements

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

Usage

Follow the instructions below:

Step 1: Initialize a ClusterManager object

let clusterManager = ClusterManager()

Step 2: Add annotations

let annotation = Annotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
annotation.style = .color(color, radius: 25) // .image(UIImage(named: "pin"))
clusterManager.add(annotation)

Step 3: Return the pins and clusters

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    if view == nil {
        view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, style: style)
    } else {
        view?.annotation = annotation
    }
    return view
}

Step 4: Reload the annotations

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    clusterManager.reload(mapView, visibleMapRect: mapView.visibleMapRect)
}

Customization

The ClusterManager exposes several properties to customize clustering:

var cellSize: Double? // The size of each cell on the grid (The larger the size, the better the performance).
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 clusterPosition: ClusterPosition // The position of the cluster annotation. The default is `.nearCenter`.

Annotations

The Annotation class exposes a style property that allows you to customize the appearance.

var style: ClusterAnnotationStyle // The style of the cluster annotation view.

You can further customize the annotations by subclassing ClusterAnnotationView and overriding configure:

override func configure(with style: ClusterAnnotationStyle) {
    super.configure(with: style)

    // customize
}

Installation

CocoaPods

To install with CocoaPods, simply add this in your Podfile:

use_frameworks!
pod "Cluster"

Carthage

To install with Carthage, simply add this in your Cartfile:

github "efremidze/Cluster"

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Credits

License

Cluster is available under the MIT license. See the LICENSE file for more info.

cluster's People

Contributors

lasha-ring avatar efremidze avatar andris-zalitis avatar eleev avatar funkenstrahlen avatar gallettube avatar the-freshlord avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.