forked from efremidze/Cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotation.swift
More file actions
executable file
·155 lines (129 loc) · 4.51 KB
/
Annotation.swift
File metadata and controls
executable file
·155 lines (129 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Annotation.swift
// Cluster
//
// Created by Lasha Efremidze on 4/15/17.
// Copyright © 2017 efremidze. All rights reserved.
//
import MapKit
open class Annotation: MKPointAnnotation {
// @available(swift, obsoleted: 6.0, message: "Please migrate to StyledClusterAnnotationView.")
open var style: ClusterAnnotationStyle?
public convenience init(coordinate: CLLocationCoordinate2D) {
self.init()
self.coordinate = coordinate
}
}
open class ClusterAnnotation: Annotation {
open var annotations = [MKAnnotation]()
open override func isEqual(_ object: Any?) -> Bool {
guard let object = object as? ClusterAnnotation else { return false }
if self === object {
return true
}
if coordinate != object.coordinate {
return false
}
if annotations.count != object.annotations.count {
return false
}
return annotations.map { $0.coordinate } == object.annotations.map { $0.coordinate }
}
}
/**
The view associated with your cluster annotations.
*/
open class ClusterAnnotationView: MKAnnotationView {
open lazy var countLabel: UILabel = {
let label = UILabel()
label.autoresizingMask = [.flexibleWidth, .flexibleHeight]
label.backgroundColor = .clear
label.font = .boldSystemFont(ofSize: 13)
label.textColor = .white
label.textAlignment = .center
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
label.baselineAdjustment = .alignCenters
self.addSubview(label)
return label
}()
open override var annotation: MKAnnotation? {
didSet {
configure()
}
}
open func configure() {
guard let annotation = annotation as? ClusterAnnotation else { return }
let count = annotation.annotations.count
countLabel.text = "\(count)"
}
}
/**
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")
}
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
}
}
}