From f93d346453d532930526f533d5e9fcfe7253355c Mon Sep 17 00:00:00 2001 From: Veronika Laskova Date: Wed, 13 Nov 2024 12:43:16 +1300 Subject: [PATCH 1/5] set icon anchor for markers in flutter web google maps --- .../google_maps_flutter_web/CHANGELOG.md | 4 +- .../integration_test/markers_test.dart | 40 +++++++++++++++++++ .../lib/src/convert.dart | 17 +++++++- .../google_maps_flutter_web/pubspec.yaml | 2 +- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md index b4239c12870e..55df6b055992 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md +++ b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md @@ -1,6 +1,6 @@ -## NEXT +## 0.5.11 -* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4. +* Adds support for marker anchor ## 0.5.10 diff --git a/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart b/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart index f76e386b1088..575989a206db 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart @@ -493,5 +493,45 @@ void main() { expect(event, isA()); expect((event as InfoWindowTapEvent).value, equals(const MarkerId('1'))); }); + + // https://github.com/flutter/flutter/issues/80578 + testWidgets('markers with anchor work', (WidgetTester tester) async { + final Uint8List bytes = const Base64Decoder().convert(iconImageBase64); + final Marker marker1 = Marker( + markerId: const MarkerId('1'), + icon: BytesMapBitmap( + bytes, + width: 20, + height: 30, + ), + ); + final Marker marker2 = Marker( + markerId: const MarkerId('2'), + icon: BytesMapBitmap( + bytes, + width: 20, + height: 30, + ), + anchor: const Offset(1.5, 2), + ); + final Set markers = {marker1, marker2}; + + await controller.addMarkers(markers); + expect(controller.markers.length, 2); + + final gmaps.Icon? icon1 = + controller.markers[const MarkerId('1')]?.marker?.icon as gmaps.Icon?; + expect(icon1, isNotNull); + final gmaps.Icon? icon2 = + controller.markers[const MarkerId('2')]?.marker?.icon as gmaps.Icon?; + expect(icon2, isNotNull); + + expect(icon1!.anchor, isNotNull); + expect(icon2!.anchor, isNotNull); + expect(icon1.anchor!.x, 20 * 0.5); + expect(icon1.anchor!.y, 30 * 1); + expect(icon2.anchor!.x, 20 * 1.5); + expect(icon2.anchor!.y, 30 * 2); + }); }); } diff --git a/packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart b/packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart index f6af32046ccd..8e6441261aac 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart @@ -296,6 +296,18 @@ void _setIconSize({ icon.scaledSize = gmapsSize; } +void _setIconAnchor({ + required Size size, + required Offset anchor, + required gmaps.Icon icon, +}) { + final gmaps.Point gmapsAnchor = gmaps.Point( + size.width * anchor.dx, + size.height * anchor.dy, + ); + icon.anchor = gmapsAnchor; +} + /// Determines the appropriate size for a bitmap based on its descriptor. /// /// This method returns the icon's size based on the provided [width] and @@ -371,7 +383,7 @@ void _cleanUpBitmapConversionCaches() { // Converts a [BitmapDescriptor] into a [gmaps.Icon] that can be used in Markers. Future _gmIconFromBitmapDescriptor( - BitmapDescriptor bitmapDescriptor) async { + BitmapDescriptor bitmapDescriptor, Offset anchor) async { gmaps.Icon? icon; if (bitmapDescriptor is MapBitmap) { @@ -394,6 +406,7 @@ Future _gmIconFromBitmapDescriptor( final Size? size = await _getBitmapSize(bitmapDescriptor, url); if (size != null) { _setIconSize(size: size, icon: icon); + _setIconAnchor(size: size, anchor: anchor, icon: icon); } case MapBitmapScaling.none: break; @@ -460,7 +473,7 @@ Future _markerOptionsFromMarker( ..visible = marker.visible ..opacity = marker.alpha ..draggable = marker.draggable - ..icon = await _gmIconFromBitmapDescriptor(marker.icon); + ..icon = await _gmIconFromBitmapDescriptor(marker.icon, marker.anchor); // TODO(ditman): Compute anchor properly, otherwise infowindows attach to the wrong spot. // Flat and Rotation are not supported directly on the web. } diff --git a/packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml b/packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml index 7ed3d99fed5e..cccec531406d 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml +++ b/packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml @@ -2,7 +2,7 @@ name: google_maps_flutter_web description: Web platform implementation of google_maps_flutter repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter_web issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22 -version: 0.5.10 +version: 0.5.11 environment: sdk: ^3.4.0 From 16b28da1aa3e886556e0ce686b372bc16aab37e3 Mon Sep 17 00:00:00 2001 From: Veronika Laskova Date: Mon, 24 Feb 2025 15:40:00 +1300 Subject: [PATCH 2/5] Use local constants for test, remove unnecessary issue link in anchor test, fixup changelog file --- .../google_maps_flutter_web/CHANGELOG.md | 6 ++++- .../integration_test/markers_test.dart | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md index 55df6b055992..4be8ff7a14d5 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md +++ b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md @@ -1,6 +1,10 @@ +## NEXT + +* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4. + ## 0.5.11 -* Adds support for marker anchor +* Adds support for marker anchor. ## 0.5.10 diff --git a/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart b/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart index 575989a206db..485829f6bd8a 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart @@ -494,25 +494,28 @@ void main() { expect((event as InfoWindowTapEvent).value, equals(const MarkerId('1'))); }); - // https://github.com/flutter/flutter/issues/80578 testWidgets('markers with anchor work', (WidgetTester tester) async { + const double width = 20; + const double height = 30; + const Offset defaultOffset = Offset(0.5, 1); + const Offset anchorOffset = Offset(1.5, 2); final Uint8List bytes = const Base64Decoder().convert(iconImageBase64); final Marker marker1 = Marker( markerId: const MarkerId('1'), icon: BytesMapBitmap( bytes, - width: 20, - height: 30, + width: width, + height: height, ), ); final Marker marker2 = Marker( markerId: const MarkerId('2'), icon: BytesMapBitmap( bytes, - width: 20, - height: 30, + width: width, + height: height, ), - anchor: const Offset(1.5, 2), + anchor: anchorOffset, ); final Set markers = {marker1, marker2}; @@ -528,10 +531,10 @@ void main() { expect(icon1!.anchor, isNotNull); expect(icon2!.anchor, isNotNull); - expect(icon1.anchor!.x, 20 * 0.5); - expect(icon1.anchor!.y, 30 * 1); - expect(icon2.anchor!.x, 20 * 1.5); - expect(icon2.anchor!.y, 30 * 2); + expect(icon1.anchor!.x, width * defaultOffset.dx); + expect(icon1.anchor!.y, height * defaultOffset.dy); + expect(icon2.anchor!.x, width * anchorOffset.dx); + expect(icon2.anchor!.y, height * anchorOffset.dy); }); }); } From 0dfee76d020f91fc1d6735664e2d04565da5aeac Mon Sep 17 00:00:00 2001 From: Veronika Laskova Date: Tue, 25 Feb 2025 08:38:44 +1300 Subject: [PATCH 3/5] Set second marker anchor offset to within image boundaries in test --- .../example/integration_test/markers_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart b/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart index 485829f6bd8a..45b218f139ca 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart @@ -498,7 +498,7 @@ void main() { const double width = 20; const double height = 30; const Offset defaultOffset = Offset(0.5, 1); - const Offset anchorOffset = Offset(1.5, 2); + const Offset anchorOffset = Offset(0, 0.5); final Uint8List bytes = const Base64Decoder().convert(iconImageBase64); final Marker marker1 = Marker( markerId: const MarkerId('1'), From 82839e45a07991fb9994ee235950377a455c3bfa Mon Sep 17 00:00:00 2001 From: Veronika Laskova Date: Tue, 4 Mar 2025 10:02:10 +1300 Subject: [PATCH 4/5] fixup changelog --- .../google_maps_flutter_web/CHANGELOG.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md index 4be8ff7a14d5..6b97b8538a5d 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md +++ b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md @@ -1,11 +1,9 @@ -## NEXT - -* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4. - ## 0.5.11 - * Adds support for marker anchor. +## 0.5.10+1 +* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4. + ## 0.5.10 * Adds support for heatmap layers. From 0f8c9a4b1fcd938bb73d96bcd12454863ebf6ffa Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Fri, 7 Mar 2025 13:45:45 -0500 Subject: [PATCH 5/5] Fix whitespace lines in changelog --- .../google_maps_flutter/google_maps_flutter_web/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md index 690c4e3f8a99..412bbef477ff 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md +++ b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.5.11 -* Adds support for marker anchor. +* Adds support for marker anchor. * Updates READMEs and API docs. * Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.