Skip to content

Commit c3a4b0f

Browse files
committed
fix: Fixed callout tap gesture
1 parent 502705d commit c3a4b0f

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

ios/Classes/Annotations/AnnotationController.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ class AnnotationController: NSObject {
108108
}
109109

110110
private func initInfoWindow(annotation: FlutterAnnotation, annotationView: MKAnnotationView) {
111-
if annotation.infoWindowConsumesTapEvents {
112-
annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
113-
}
114111
if #available(iOS 9.0, *) {
115112
let lines = annotation.subtitle?.split(whereSeparator: \.isNewline)
116113
if lines != nil {

ios/Classes/MapView/AppleMapController.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class AppleMapController : NSObject, FlutterPlatformView, MKMapViewDelega
4040
var circleController: CircleController
4141
var initialCameraPosition: [String: Any]
4242
var options: [String: Any]
43+
var onCalloutTapGestureRecognizer: UITapGestureRecognizer?
44+
var currentlySelectedAnnotation: String?
4345

4446
public init(withFrame frame: CGRect, withRegistrar registrar: FlutterPluginRegistrar, withargs args: Dictionary<String, Any> ,withId id: Int64) {
4547
self.options = args["options"] as! [String: Any]
@@ -72,18 +74,14 @@ public class AppleMapController : NSObject, FlutterPlatformView, MKMapViewDelega
7274
if let circlesToAdd: NSArray = args["circlesToAdd"] as? NSArray {
7375
self.circleController.addCircles(circleData: circlesToAdd)
7476
}
77+
78+
self.onCalloutTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.calloutTapped(_:)))
7579
}
7680

7781
public func view() -> UIView {
7882
return mapView
7983
}
8084

81-
public func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
82-
if let flutterAnnotation: FlutterAnnotation = view.annotation as? FlutterAnnotation {
83-
self.channel.invokeMethod("infoWindow#onTap", arguments: ["annotationId": flutterAnnotation.id])
84-
}
85-
}
86-
8785
// onIdle
8886
public func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
8987
self.channel.invokeMethod("camera#onIdle", arguments: "")
@@ -95,11 +93,20 @@ public class AppleMapController : NSObject, FlutterPlatformView, MKMapViewDelega
9593
}
9694

9795
public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
96+
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.calloutTapped(_:)))
97+
view.addGestureRecognizer(tapGesture)
9898
if let annotation :FlutterAnnotation = view.annotation as? FlutterAnnotation {
99+
self.currentlySelectedAnnotation = annotation.id
99100
self.annotationController.onAnnotationClick(annotation: annotation)
100101
}
101102
}
102103

104+
public func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
105+
self.currentlySelectedAnnotation = nil
106+
view.removeGestureRecognizer(self.onCalloutTapGestureRecognizer!)
107+
}
108+
109+
103110
public func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
104111
if annotation is MKUserLocation {
105112
return nil
@@ -120,6 +127,12 @@ public class AppleMapController : NSObject, FlutterPlatformView, MKMapViewDelega
120127
return MKOverlayRenderer()
121128
}
122129

130+
@objc func calloutTapped(_ sender: UITapGestureRecognizer? = nil) {
131+
if self.currentlySelectedAnnotation != nil {
132+
self.channel.invokeMethod("infoWindow#onTap", arguments: ["annotationId": self.currentlySelectedAnnotation!])
133+
}
134+
}
135+
123136
private func setMethodCallHandlers() {
124137
channel.setMethodCallHandler({(call: FlutterMethodCall, result: FlutterResult) -> Void in
125138
if let args :Dictionary<String, Any> = call.arguments as? Dictionary<String,Any> {

0 commit comments

Comments
 (0)