8
8
import Foundation
9
9
import MapKit
10
10
11
- class AnnotationController : NSObject {
11
+ extension AppleMapController : AnnotationDelegate {
12
12
13
- let mapView : MKMapView
14
- let channel : FlutterMethodChannel
15
- let registrar : FlutterPluginRegistrar
16
-
17
- public init ( mapView : MKMapView , channel : FlutterMethodChannel , registrar: FlutterPluginRegistrar ) {
18
- self . mapView = mapView
19
- self . channel = channel
20
- self . registrar = registrar
21
- }
22
-
23
- func getAnnotationView( annotation: FlutterAnnotation ) -> MKAnnotationView {
24
- let identifier : String = annotation. id
13
+ func getAnnotationView( annotation: FlutterAnnotation ) -> MKAnnotationView {
14
+ let identifier : String = annotation. id
25
15
var annotationView = self . mapView. dequeueReusableAnnotationView ( withIdentifier: identifier)
26
16
let oldflutterAnnoation = annotationView? . annotation as? FlutterAnnotation
27
17
if annotationView == nil || oldflutterAnnoation? . icon. iconType != annotation. icon. iconType {
@@ -58,15 +48,15 @@ class AnnotationController: NSObject {
58
48
return annotationView!
59
49
}
60
50
61
- public func annotationsToAdd( annotations : NSArray ) {
51
+ func annotationsToAdd( annotations : NSArray ) {
62
52
for annotation in annotations {
63
53
let annotationData : Dictionary < String , Any > = annotation as! Dictionary < String , Any >
64
54
addAnnotation ( annotationData: annotationData)
65
55
}
66
56
}
67
57
68
- public func annotationsToChange( annotations: NSArray ) {
69
- let oldAnnotations : [ MKAnnotation ] = mapView. annotations
58
+ func annotationsToChange( annotations: NSArray ) {
59
+ let oldAnnotations : [ MKAnnotation ] = self . mapView. annotations
70
60
for annotation in annotations {
71
61
let annotationData : Dictionary < String , Any > = annotation as! Dictionary < String , Any >
72
62
for oldAnnotation in oldAnnotations {
@@ -86,30 +76,55 @@ class AnnotationController: NSObject {
86
76
}
87
77
}
88
78
89
- public func annotationsIdsToRemove( annotationIds: NSArray ) {
79
+ func annotationsIdsToRemove( annotationIds: NSArray ) {
90
80
for annotationId in annotationIds {
91
81
if let _annotationId : String = annotationId as? String {
92
82
removeAnnotation ( id: _annotationId)
93
83
}
94
84
}
95
85
}
86
+
87
+ func removeAllAnnotations( ) {
88
+ self . mapView. removeAnnotations ( self . mapView. annotations)
89
+ }
96
90
97
- public func onAnnotationClick( annotation : MKAnnotation ) {
91
+ func onAnnotationClick( annotation : MKAnnotation ) {
98
92
if let flutterAnnotation : FlutterAnnotation = annotation as? FlutterAnnotation {
99
93
flutterAnnotation. wasDragged = true
100
94
channel. invokeMethod ( " annotation#onTap " , arguments: [ " annotationId " : flutterAnnotation. id] )
101
95
}
102
96
}
97
+
98
+ func showAnnotation( with id: String ) {
99
+ let annotation = self . getAnnotation ( with: id)
100
+ guard annotation != nil else {
101
+ return
102
+ }
103
+ self . mapView. selectAnnotation ( annotation!, animated: true )
104
+ }
105
+
106
+ func hideAnnotation( with id: String ) {
107
+ let annotation = self . getAnnotation ( with: id)
108
+ guard annotation != nil else {
109
+ return
110
+ }
111
+ self . mapView. deselectAnnotation ( annotation!, animated: true )
112
+ }
113
+
114
+ func isAnnotationSelected( with id: String ) -> Bool {
115
+ return self . mapView. selectedAnnotations. contains ( where: { annotation in return self . getAnnotation ( with: id) == ( annotation as? FlutterAnnotation ) } )
116
+ }
117
+
103
118
104
119
private func removeAnnotation( id: String ) {
105
120
if let flutterAnnotation : FlutterAnnotation = self . getAnnotation ( with: id) {
106
- mapView. removeAnnotation ( flutterAnnotation)
121
+ self . mapView. removeAnnotation ( flutterAnnotation)
107
122
}
108
123
}
109
124
110
125
private func updateAnnotationOnMap( oldAnnotation: FlutterAnnotation , newAnnotation : FlutterAnnotation ) {
111
126
removeAnnotation ( id: oldAnnotation. id)
112
- mapView. addAnnotation ( newAnnotation)
127
+ self . mapView. addAnnotation ( newAnnotation)
113
128
}
114
129
115
130
private func initInfoWindow( annotation: FlutterAnnotation , annotationView: MKAnnotationView ) {
@@ -133,33 +148,13 @@ class AnnotationController: NSObject {
133
148
}
134
149
}
135
150
136
- public func showAnnotation( with id: String ) {
137
- let annotation = self . getAnnotation ( with: id)
138
- guard annotation != nil else {
139
- return
140
- }
141
- self . mapView. selectAnnotation ( annotation!, animated: true )
142
- }
143
-
144
- public func hideAnnotation( with id: String ) {
145
- let annotation = self . getAnnotation ( with: id)
146
- guard annotation != nil else {
147
- return
148
- }
149
- self . mapView. deselectAnnotation ( annotation!, animated: true )
150
- }
151
-
152
- public func isAnnotationSelected( with id: String ) -> Bool {
153
- return self . mapView. selectedAnnotations. contains ( where: { annotation in return self . getAnnotation ( with: id) == ( annotation as? FlutterAnnotation ) } )
154
- }
155
-
156
151
private func getAnnotation( with id: String ) -> FlutterAnnotation ? {
157
152
return self . mapView. annotations. filter { annotation in return ( annotation as? FlutterAnnotation ) ? . id == id } . first as? FlutterAnnotation
158
153
}
159
154
160
155
private func addAnnotation( annotationData: Dictionary < String , Any > ) {
161
156
let annotation : MKAnnotation = FlutterAnnotation ( fromDictionary: annotationData, registrar: registrar)
162
- mapView. addAnnotation ( annotation)
157
+ self . mapView. addAnnotation ( annotation)
163
158
}
164
159
165
160
private func getPinAnnotationView( annotation: MKAnnotation , id: String ) -> MKPinAnnotationView {
0 commit comments