Skip to content

Commit e037d19

Browse files
[google_maps_flutter_web] set icon anchor for markers (#8077)
*Fixes issue where a marker anchor is ignored on flutter web google maps.* *flutter/flutter#80578
1 parent 4c5a7ed commit e037d19

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## 0.5.10+1
1+
## 0.5.11
22

3+
* Adds support for marker anchor.
34
* Updates READMEs and API docs.
45
* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.
56

packages/google_maps_flutter/google_maps_flutter_web/example/integration_test/markers_test.dart

+43
Original file line numberDiff line numberDiff line change
@@ -493,5 +493,48 @@ void main() {
493493
expect(event, isA<InfoWindowTapEvent>());
494494
expect((event as InfoWindowTapEvent).value, equals(const MarkerId('1')));
495495
});
496+
497+
testWidgets('markers with anchor work', (WidgetTester tester) async {
498+
const double width = 20;
499+
const double height = 30;
500+
const Offset defaultOffset = Offset(0.5, 1);
501+
const Offset anchorOffset = Offset(0, 0.5);
502+
final Uint8List bytes = const Base64Decoder().convert(iconImageBase64);
503+
final Marker marker1 = Marker(
504+
markerId: const MarkerId('1'),
505+
icon: BytesMapBitmap(
506+
bytes,
507+
width: width,
508+
height: height,
509+
),
510+
);
511+
final Marker marker2 = Marker(
512+
markerId: const MarkerId('2'),
513+
icon: BytesMapBitmap(
514+
bytes,
515+
width: width,
516+
height: height,
517+
),
518+
anchor: anchorOffset,
519+
);
520+
final Set<Marker> markers = <Marker>{marker1, marker2};
521+
522+
await controller.addMarkers(markers);
523+
expect(controller.markers.length, 2);
524+
525+
final gmaps.Icon? icon1 =
526+
controller.markers[const MarkerId('1')]?.marker?.icon as gmaps.Icon?;
527+
expect(icon1, isNotNull);
528+
final gmaps.Icon? icon2 =
529+
controller.markers[const MarkerId('2')]?.marker?.icon as gmaps.Icon?;
530+
expect(icon2, isNotNull);
531+
532+
expect(icon1!.anchor, isNotNull);
533+
expect(icon2!.anchor, isNotNull);
534+
expect(icon1.anchor!.x, width * defaultOffset.dx);
535+
expect(icon1.anchor!.y, height * defaultOffset.dy);
536+
expect(icon2.anchor!.x, width * anchorOffset.dx);
537+
expect(icon2.anchor!.y, height * anchorOffset.dy);
538+
});
496539
});
497540
}

packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart

+15-2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,18 @@ void _setIconSize({
296296
icon.scaledSize = gmapsSize;
297297
}
298298

299+
void _setIconAnchor({
300+
required Size size,
301+
required Offset anchor,
302+
required gmaps.Icon icon,
303+
}) {
304+
final gmaps.Point gmapsAnchor = gmaps.Point(
305+
size.width * anchor.dx,
306+
size.height * anchor.dy,
307+
);
308+
icon.anchor = gmapsAnchor;
309+
}
310+
299311
/// Determines the appropriate size for a bitmap based on its descriptor.
300312
///
301313
/// This method returns the icon's size based on the provided [width] and
@@ -371,7 +383,7 @@ void _cleanUpBitmapConversionCaches() {
371383

372384
// Converts a [BitmapDescriptor] into a [gmaps.Icon] that can be used in Markers.
373385
Future<gmaps.Icon?> _gmIconFromBitmapDescriptor(
374-
BitmapDescriptor bitmapDescriptor) async {
386+
BitmapDescriptor bitmapDescriptor, Offset anchor) async {
375387
gmaps.Icon? icon;
376388

377389
if (bitmapDescriptor is MapBitmap) {
@@ -394,6 +406,7 @@ Future<gmaps.Icon?> _gmIconFromBitmapDescriptor(
394406
final Size? size = await _getBitmapSize(bitmapDescriptor, url);
395407
if (size != null) {
396408
_setIconSize(size: size, icon: icon);
409+
_setIconAnchor(size: size, anchor: anchor, icon: icon);
397410
}
398411
case MapBitmapScaling.none:
399412
break;
@@ -460,7 +473,7 @@ Future<gmaps.MarkerOptions> _markerOptionsFromMarker(
460473
..visible = marker.visible
461474
..opacity = marker.alpha
462475
..draggable = marker.draggable
463-
..icon = await _gmIconFromBitmapDescriptor(marker.icon);
476+
..icon = await _gmIconFromBitmapDescriptor(marker.icon, marker.anchor);
464477
// TODO(ditman): Compute anchor properly, otherwise infowindows attach to the wrong spot.
465478
// Flat and Rotation are not supported directly on the web.
466479
}

packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_maps_flutter_web
22
description: Web platform implementation of google_maps_flutter
33
repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
5-
version: 0.5.10+1
5+
version: 0.5.11
66

77
environment:
88
sdk: ^3.4.0

0 commit comments

Comments
 (0)