Skip to content

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
efremidze committed Apr 30, 2017
1 parent 32422f7 commit e283440
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 103 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 1.0.3](https://github.com/efremidze/Cluster/releases/tag/1.0.3)
Released on 2017-04-29

- Added image support

## [Version 1.0.2](https://github.com/efremidze/Cluster/releases/tag/1.0.2)
Released on 2017-04-19

Expand Down
2 changes: 1 addition & 1 deletion Cluster.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Cluster'
s.version = '1.0.2'
s.version = '1.0.3'
s.summary = 'Map Clustering Library'
s.homepage = 'https://github.com/efremidze/Cluster'
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
25 changes: 25 additions & 0 deletions Example/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -30,6 +40,16 @@
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
Expand Down Expand Up @@ -59,6 +79,11 @@
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
Expand Down
6 changes: 6 additions & 0 deletions Example/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
15 changes: 15 additions & 0 deletions Example/Assets.xcassets/pin.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "pin.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"template-rendering-intent" : "template"
}
}
Binary file added Example/Assets.xcassets/pin.imageset/pin.pdf
Binary file not shown.
36 changes: 33 additions & 3 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let annotations: [Annotation] = (0..<1000).map { _ in
let annotations: [Annotation] = (0..<1000).map { i in
let annotation = Annotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: drand48() * 80 - 40, longitude: drand48() * 80 - 40)
let color = UIColor(red: 255/255, green: 149/255, blue: 0/255, alpha: 1)
if i % 2 == 0 {
annotation.type = .color(color, radius: 25)
} else {
annotation.type = .image(UIImage(named: "pin")?.filled(with: color))
}
return annotation
}
manager.add(annotations)
Expand All @@ -35,11 +41,15 @@ extension ViewController: MKMapViewDelegate {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let color = UIColor(red: 255/255, green: 149/255, blue: 0/255, alpha: 1)
if annotation is ClusterAnnotation {
if let annotation = annotation as? ClusterAnnotation {
let identifier = "Cluster"
var view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if view == nil {
view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, type: .color(color: color, radius: 25))
if let annotation = annotation.annotations.first as? Annotation, let type = annotation.type {
view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, type: type)
} else {
view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, type: .color(color, radius: 25))
}
} else {
view?.annotation = annotation
}
Expand Down Expand Up @@ -83,3 +93,23 @@ extension ViewController: MKMapViewDelegate {
}

}

extension UIImage {

func filled(with color: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
color.setFill()
guard let context = UIGraphicsGetCurrentContext() else { return self }
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0);
context.setBlendMode(CGBlendMode.normal)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
guard let mask = self.cgImage else { return self }
context.clip(to: rect, mask: mask)
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}

}
78 changes: 0 additions & 78 deletions Example/ViewController2.swift

This file was deleted.

Binary file modified Images/demo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ let clusterManager = ClusterManager()
```swift
let annotation = Annotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
annotation.type = .color(color, radius: 25) // .image(UIImage(named: "pin"))
clusterManager.add(annotation)
```

Expand All @@ -60,7 +61,7 @@ clusterManager.add(annotation)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if view == nil {
view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, type: .color(color: color, radius: radius))
view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, type: type)
} else {
view?.annotation = annotation
}
Expand All @@ -72,14 +73,10 @@ func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnota

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

### Customization

To use images for your annotation cluster view take a look at [Example2](Example/ViewController2.swift).

## Installation

### CocoaPods
Expand Down
31 changes: 17 additions & 14 deletions Sources/Annotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ open class Annotation: NSObject, MKAnnotation {
open var coordinate = CLLocationCoordinate2D()
open var title: String?
open var subtitle: String?
open var type: ClusterAnnotationType?
}

open class ClusterAnnotation: Annotation {
open var annotations = [MKAnnotation]()
}

public enum ClusterAnnotationType {
case color(color: UIColor, radius: CGFloat)
case image(named: String)
case color(UIColor, radius: CGFloat)
case image(UIImage?)
}

open class ClusterAnnotationView: MKAnnotationView {
Expand All @@ -44,7 +45,7 @@ open class ClusterAnnotationView: MKAnnotationView {
}
}

public var type: ClusterAnnotationType = .color(color: .red, radius: 25) {
public var type: ClusterAnnotationType? {
didSet {
configure()
}
Expand Down Expand Up @@ -72,10 +73,10 @@ open class ClusterAnnotationView: MKAnnotationView {
let count = annotation.annotations.count

switch type {
case let .image(named):
case let .image(image)?:
backgroundColor = .clear
image = UIImage(named: named)
case let .color(color, radius):
self.image = image
case let .color(color, radius)?:
backgroundColor = color
var diameter = radius * 2
switch count {
Expand All @@ -86,20 +87,22 @@ open class ClusterAnnotationView: MKAnnotationView {
default: break
}
frame = CGRect(origin: frame.origin, size: CGSize(width: diameter, height: diameter))
layer.borderColor = UIColor.white.cgColor
layer.borderWidth = 2
countLabel.font = .boldSystemFont(ofSize: 13)
countLabel.text = "\(count)"
default: break
}

layer.borderColor = UIColor.white.cgColor
layer.borderWidth = 2
countLabel.font = .boldSystemFont(ofSize: 13)
countLabel.text = "\(count)"
}

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

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

}
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>1.0.2</string>
<string>1.0.3</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down

0 comments on commit e283440

Please sign in to comment.