Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[google_maps_flutter_web] set icon anchor for markers #8077

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,5 +493,45 @@ void main() {
expect(event, isA<InfoWindowTapEvent>());
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<Marker> markers = <Marker>{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);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -371,7 +383,7 @@ void _cleanUpBitmapConversionCaches() {

// Converts a [BitmapDescriptor] into a [gmaps.Icon] that can be used in Markers.
Future<gmaps.Icon?> _gmIconFromBitmapDescriptor(
BitmapDescriptor bitmapDescriptor) async {
BitmapDescriptor bitmapDescriptor, Offset anchor) async {
gmaps.Icon? icon;

if (bitmapDescriptor is MapBitmap) {
Expand All @@ -394,6 +406,7 @@ Future<gmaps.Icon?> _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;
Expand Down Expand Up @@ -460,7 +473,7 @@ Future<gmaps.MarkerOptions> _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.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down