Uh oh!
There was an error while loading. Please reload this page.
forked from efremidze/Cluster
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
Latest commit
executable file
Β·155 lines (130 loc) Β· 5.66 KB
/
Copy pathViewController.swift
File metadata and controls
executable file
Β·155 lines (130 loc) Β· 5.66 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
//
// ViewController.swift
// Example
//
// Created by Lasha Efremidze on 4/13/17.
// Copyright Β© 2017 efremidze. All rights reserved.
//
import UIKit
import MapKit
import Cluster
classViewController:UIViewController{
@IBOutlet weak varmapView:MKMapView!{
didSet {
mapView.region =.init(center: region.center, span:.init(latitudeDelta: region.delta, longitudeDelta: region.delta))
}
}
@IBOutlet weak varsegmentedControl:UISegmentedControl!
lazy varmanager:ClusterManager={[unowned self]in
letmanager=ClusterManager()
manager.delegate =self
manager.maxZoomLevel =17
manager.minCountForClustering =3
manager.clusterPosition =.nearCenter
return manager
}()
letregion=(center:CLLocationCoordinate2D(latitude:37.787994, longitude:-122.407437), delta:0.1)
overridefunc viewDidLoad(){
super.viewDidLoad()
manager.add(MeAnnotation(coordinate: region.center))
addAnnotations()
}
@IBActionfunc addAnnotations(_ sender:UIButton?=nil){
// Add annotations to the manager.
letannotations:[Annotation]=(0..<100000).map{ i in
letannotation=Annotation()
annotation.coordinate =CLLocationCoordinate2D(latitude: region.center.latitude + drand48()* region.delta - region.delta /2, longitude: region.center.longitude + drand48()* region.delta - region.delta /2)
return annotation
}
manager.add(annotations)
manager.reload(mapView: mapView)
}
@IBActionfunc removeAnnotations(_ sender:UIButton?=nil){
manager.removeAll()
manager.reload(mapView: mapView)
}
@IBActionfunc valueChanged(_ sender:UISegmentedControl){
removeAnnotations()
addAnnotations()
}
}
extensionViewController:MKMapViewDelegate{
func mapView(_ mapView:MKMapView, viewFor annotation:MKAnnotation)->MKAnnotationView?{
iflet annotation = annotation as?ClusterAnnotation{
letindex= segmentedControl.selectedSegmentIndex
letidentifier="Cluster\(index)"
letselection=Selection(rawValue: index)!
return mapView.annotationView(selection: selection, annotation: annotation, reuseIdentifier: identifier)
}elseiflet annotation = annotation as?MeAnnotation{
letidentifier="Me"
letannotationView= mapView.annotationView(of:MKAnnotationView.self, annotation: annotation, reuseIdentifier: identifier)
annotationView.image =UIImage.me.filled(with:.blue)
return annotationView
}else{
letidentifier="Pin"
letannotationView= mapView.annotationView(of:MKPinAnnotationView.self, annotation: annotation, reuseIdentifier: identifier)
annotationView.pinTintColor =.green
return annotationView
}
}
func mapView(_ mapView:MKMapView, regionDidChangeAnimated animated:Bool){
manager.reload(mapView: mapView){ finished in
print(finished)
}
}
func mapView(_ mapView:MKMapView, didSelect view:MKAnnotationView){
guardlet annotation = view.annotation else{return}
iflet cluster = annotation as?ClusterAnnotation{
varzoomRect=MKMapRect.null
forannotationin cluster.annotations {
letannotationPoint=MKMapPoint(annotation.coordinate)
letpointRect=MKMapRect(x: annotationPoint.x, y: annotationPoint.y, width:0, height:0)
if zoomRect.isNull {
zoomRect = pointRect
}else{
zoomRect = zoomRect.union(pointRect)
}
}
mapView.setVisibleMapRect(zoomRect, animated:true)
}
}
func mapView(_ mapView:MKMapView, didAdd views:[MKAnnotationView]){
views.forEach{ $0.alpha =0}
UIView.animate(withDuration:0.35, delay:0, usingSpringWithDamping:1, initialSpringVelocity:0, options:[], animations:{
views.forEach{ $0.alpha =1}
}, completion:nil)
}
}
extensionViewController:ClusterManagerDelegate{
func cellSize(for zoomLevel:Double)->Double?{
returnnil // default
}
func shouldClusterAnnotation(_ annotation:MKAnnotation)->Bool{
return !(annotation is MeAnnotation)
}
}
extensionViewController{
enumSelection:Int{
case count, imageCount, image
}
}
extensionMKMapView{
func annotationView(selection:ViewController.Selection, annotation:MKAnnotation?, reuseIdentifier:String)->MKAnnotationView{
switch selection {
case.count:
letannotationView=self.annotationView(of:CountClusterAnnotationView.self, annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView.countLabel.backgroundColor =.green
return annotationView
case.imageCount:
letannotationView=self.annotationView(of:ImageCountClusterAnnotationView.self, annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView.countLabel.textColor =.green
annotationView.image =UIImage.pin2.filled(with:.green)
return annotationView
case.image:
letannotationView=self.annotationView(of:MKAnnotationView.self, annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView.image =UIImage.pin.filled(with:.green)
return annotationView
}
}
}
classMeAnnotation:Annotation{}