Skip to content

Commit b7403b2

Browse files
committed
feat: Added configurable anchor for infoWindows
1 parent 7268f75 commit b7403b2

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

example/lib/place_annotation.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class PlaceAnnotationBodyState extends State<PlaceAnnotationBody> {
9393
),
9494
infoWindow: InfoWindow(
9595
title: annotationIdVal,
96+
anchor: Offset(0.5, 0.0),
9697
snippet: '*',
9798
onTap: () => print('InfowWindow of id: $annotationId tapped.')),
9899
onTap: () {
@@ -221,7 +222,7 @@ class PlaceAnnotationBodyState extends State<PlaceAnnotationBody> {
221222
@override
222223
Widget build(BuildContext context) {
223224
_createAnnotationImageFromAsset(context, _devicePixelRatio);
224-
_getBytesFromAsset('assets/red_square.png', 10);
225+
_getBytesFromAsset('assets/red_square.png', 40);
225226
return Column(
226227
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
227228
crossAxisAlignment: CrossAxisAlignment.stretch,

ios/Classes/Annotations/AnnotationController.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ class AnnotationController: NSObject {
111111
}
112112

113113
private func initInfoWindow(annotation: FlutterAnnotation, annotationView: MKAnnotationView) {
114+
\(CGFloat(annotation.calloutOffset.y))")
115+
let x = self.getInfoWindowXOffset(annotationView: annotationView, annotation: annotation)
116+
let y = self.getInfoWindowYOffset(annotationView: annotationView, annotation: annotation)
117+
annotationView.calloutOffset = CGPoint(x: x, y: y)
114118
if #available(iOS 9.0, *) {
115119
let lines = annotation.subtitle?.split(whereSeparator: \.isNewline)
116120
if lines != nil {
@@ -186,4 +190,15 @@ class AnnotationController: NSObject {
186190
annotationView?.image = annotation.icon.image
187191
return annotationView!
188192
}
193+
194+
private func getInfoWindowXOffset(annotationView: MKAnnotationView, annotation: FlutterAnnotation) -> CGFloat {
195+
if annotation.icon.iconType == .PIN {
196+
return annotationView.frame.origin.x - (annotationView.frame.origin.x * CGFloat(annotation.calloutOffset.x))
197+
}
198+
return annotationView.frame.origin.x + (annotationView.frame.width * CGFloat(annotation.calloutOffset.x))
199+
}
200+
201+
private func getInfoWindowYOffset(annotationView: MKAnnotationView, annotation: FlutterAnnotation) -> CGFloat {
202+
return annotationView.frame.height * CGFloat(annotation.calloutOffset.y)
203+
}
189204
}

ios/Classes/Annotations/FlutterAnnotation.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class FlutterAnnotation: NSObject, MKAnnotation {
1919
var isDraggable: Bool?
2020
var wasDragged: Bool = false
2121
var isVisible: Bool? = true
22+
var calloutOffset: CalloutOffset = CalloutOffset(x: 0, y: 0)
2223
var icon: AnnotationIcon = AnnotationIcon.init()
2324

2425
public init(fromDictionary annotationData: Dictionary<String, Any>, registrar: FlutterPluginRegistrar) {
@@ -39,6 +40,10 @@ class FlutterAnnotation: NSObject, MKAnnotation {
3940
if let iconData: Array<Any> = annotationData["icon"] as? Array<Any> {
4041
self.icon = FlutterAnnotation.getAnnotationIcon(iconData: iconData, registrar: registrar, annotationId: id)
4142
}
43+
44+
if let calloutOffsetJSON = infoWindow["anchor"] as? Array<Double> {
45+
self.calloutOffset = CalloutOffset(x: calloutOffsetJSON[0], y: calloutOffsetJSON[1])
46+
}
4247
}
4348

4449
static private func getAnnotationIcon(iconData: Array<Any>, registrar: FlutterPluginRegistrar, annotationId: String) -> AnnotationIcon {
@@ -69,3 +74,8 @@ class FlutterAnnotation: NSObject, MKAnnotation {
6974
return !(lhs == rhs)
7075
}
7176
}
77+
78+
struct CalloutOffset {
79+
let x: Double
80+
let y: Double
81+
}

ios/Classes/MapView/AppleMapController.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ public class AppleMapController : NSObject, FlutterPlatformView, MKMapViewDelega
9393
}
9494

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

0 commit comments

Comments
 (0)