Skip to content

Commit

Permalink
2.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
efremidze committed Jan 29, 2019
1 parent 11cfbcc commit c9470a2
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

## [Version 2.4.3](https://github.com/efremidze/Cluster/releases/tag/2.4.3)
Released on 2019-01-28

- Reintroduced StyledClusterAnnotationView

## [Version 2.4.2](https://github.com/efremidze/Cluster/releases/tag/2.4.2)
Released on 2019-01-07

Expand Down
2 changes: 1 addition & 1 deletion Cluster.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Cluster'
s.version = '2.4.2'
s.version = '2.4.3'
s.summary = 'Map Clustering Library'
s.homepage = 'https://github.com/efremidze/Cluster'
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,30 @@ class CountClusterAnnotationView: ClusterAnnotationView {

See the [AnnotationView](Example/AnnotationView.swift) to learn more.

### Annotation Styling

You can customize the appearance of the `StyledClusterAnnotationView` by setting the `style` property of the annotation.

```swift
let annotation = Annotation(coordinate: CLLocationCoordinate2D(latitude: 21.283921, longitude: -157.831661))
annotation.style = .color(color, radius: 25)
manager.add(annotation)
```

Several styles are available in the `ClusterAnnotationStyle` enum:
- `color(UIColor, radius: CGFloat)` - Displays the annotations as a circle.
- `image(UIImage?)` - Displays the annotation as an image.

Once you have added the annotation, you need to return an instance of the `StyledClusterAnnotationView` to display the styled annotation.

```swift
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? ClusterAnnotation {
return StyledClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, style: style)
}
}
```

## Removing Annotations

To remove annotations, you can call `remove(annotation:)`. However the annotations will still display until you call `reload()`.
Expand Down
95 changes: 95 additions & 0 deletions Sources/Annotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import MapKit

open class Annotation: MKPointAnnotation {
// @available(*, deprecated: 2.5.0, message: "See docs/MigratingToTransforms.md")
open var style: ClusterAnnotationStyle?

public convenience init(coordinate: CLLocationCoordinate2D) {
self.init()
self.coordinate = coordinate
Expand Down Expand Up @@ -37,6 +40,9 @@ open class ClusterAnnotation: Annotation {
}
}

/**
The view associated with your cluster annotations.
*/
open class ClusterAnnotationView: MKAnnotationView {

open lazy var countLabel: UILabel = {
Expand Down Expand Up @@ -66,3 +72,92 @@ open class ClusterAnnotationView: MKAnnotationView {
}

}

/**
The style of the cluster annotation view.
*/
public enum ClusterAnnotationStyle {
/**
Displays the annotations as a circle.
- `color`: The color of the annotation circle
- `radius`: The radius of the annotation circle
*/
case color(UIColor, radius: CGFloat)

/**
Displays the annotation as an image.
*/
case image(UIImage?)
}

/**
A cluster annotation view that supports styles.
*/
open class StyledClusterAnnotationView: ClusterAnnotationView {

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

/**
Initializes and returns a new cluster annotation view.
- Parameters:
- annotation: The annotation object to associate with the new view.
- reuseIdentifier: If you plan to reuse the annotation view for similar types of annotations, pass a string to identify it. Although you can pass nil if you do not intend to reuse the view, reusing annotation views is generally recommended.
- style: The cluster annotation style to associate with the new view.
- Returns: The initialized cluster annotation view.
*/
public init(annotation: MKAnnotation?, reuseIdentifier: String?, style: ClusterAnnotationStyle) {
self.style = style
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
configure()
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@available(*, deprecated: 2.2.5, message:"Use configure()")
open func configure(with style: ClusterAnnotationStyle) {
self.style = style
configure()
}

open override func configure() {
guard let annotation = annotation as? ClusterAnnotation else { return }

switch style {
case let .image(image):
backgroundColor = .clear
self.image = image
case let .color(color, radius):
let count = annotation.annotations.count
backgroundColor = color
var diameter = radius * 2
switch count {
case _ where count < 8:
diameter *= 0.6
case _ where count < 16:
diameter *= 0.8
default: break
}
frame = CGRect(origin: frame.origin, size: CGSize(width: diameter, height: diameter))
countLabel.text = "\(count)"
}
}

override open func layoutSubviews() {
super.layoutSubviews()

if case .color = style {
layer.masksToBounds = true
layer.cornerRadius = image == nil ? bounds.width / 2 : 0
countLabel.frame = bounds
}
}

}
1 change: 1 addition & 0 deletions Sources/Cluster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ open class ClusterManager {
}
}
cluster.annotations = annotations
cluster.style = (annotations.first as? Annotation)?.style
allAnnotations += [cluster]
} else {
allAnnotations += annotations
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.4.2</string>
<string>2.4.3</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down

0 comments on commit c9470a2

Please sign in to comment.