Skip to content

Commit f93d346

Browse files
set icon anchor for markers in flutter web google maps
1 parent 8ecf4b0 commit f93d346

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## NEXT
1+
## 0.5.11
22

3-
* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.
3+
* Adds support for marker anchor
44

55
## 0.5.10
66

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

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

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

Lines changed: 15 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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
5+
version: 0.5.11
66

77
environment:
88
sdk: ^3.4.0

0 commit comments

Comments
 (0)