Skip to content
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,3 +1,7 @@
## 0.6.2+4

* Fix AdvancedMarker anchor handling on web.

## 0.6.2+3

* Updates README to include setup information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this using unsafe dynamic lookup? If there are properties that aren't reflected in google_maps, they should be added upstream, rather than bypassing the wrapper.

import 'dart:typed_data';

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -377,6 +378,32 @@ void main() {
expect(icon.style.height, '30px');
});

testWidgets('markers with anchor work', (WidgetTester tester) async {
const markerId = MarkerId('1');
const anchorOffset = Offset(0.25, 0.75);
const updatedAnchorOffset = Offset(-0.6, 1.6);

final markers = <AdvancedMarker>{AdvancedMarker(markerId: markerId, anchor: anchorOffset)};

await controller.addMarkers(markers);

gmaps.AdvancedMarkerElement? marker = controller.markers[markerId]?.marker;
expect(marker, isNotNull);
expect((marker!.getProperty('anchorLeft'.toJS)! as JSString).toDart, '-25%');
expect((marker.getProperty('anchorTop'.toJS)! as JSString).toDart, '-75%');

final updatedMarkers = <AdvancedMarker>{
AdvancedMarker(markerId: markerId, anchor: updatedAnchorOffset),
};

await controller.changeMarkers(updatedMarkers);

marker = controller.markers[markerId]?.marker;
expect(marker, isNotNull);
expect((marker!.getProperty('anchorLeft'.toJS)! as JSString).toDart, '60%');
expect((marker.getProperty('anchorTop'.toJS)! as JSString).toDart, '-160%');
});

testWidgets('markers created with text glyph work', (WidgetTester widgetTester) async {
final markers = <AdvancedMarker>{
AdvancedMarker(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ library google_maps_flutter_web;
import 'dart:async';
import 'dart:convert';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'dart:ui_web' as ui_web;

import 'package:collection/collection.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,35 @@ void _setIconAnchor({required gmaps.Size size, required Offset anchor, required
icon.anchor = gmapsAnchor;
}

String _advancedMarkerAnchorToCssOffset(double anchor) {
final double percentage = -anchor * 100;
if (percentage == percentage.roundToDouble()) {
return '${percentage.toInt()}%';
}
return '$percentage%';
}
Comment on lines +370 to +376

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Floating-point multiplication (e.g., -anchor * 100) can sometimes introduce precision artifacts (for example, -0.14 * 100 results in -14.000000000000002 in Dart/JS). This can cause the strict equality check percentage == percentage.roundToDouble() to fail, leading to unnecessarily verbose CSS percentage strings like '-14.000000000000002%' instead of '-14%'.

Using a small epsilon check when comparing the percentage to its rounded integer value will make the output cleaner and more robust against floating-point precision issues.

Suggested change
String _advancedMarkerAnchorToCssOffset(double anchor) {
final double percentage = -anchor * 100;
if (percentage == percentage.roundToDouble()) {
return '${percentage.toInt()}%';
}
return '$percentage%';
}
String _advancedMarkerAnchorToCssOffset(double anchor) {
final double percentage = -anchor * 100;
final int rounded = percentage.round();
if ((percentage - rounded).abs() < 1e-9) {
return '$rounded%';
}
return '$percentage%';
}


void _setAdvancedMarkerOptionsAnchor(gmaps.AdvancedMarkerElementOptions options, Offset anchor) {
options
..setProperty('anchorLeft'.toJS, _advancedMarkerAnchorToCssOffset(anchor.dx).toJS)
..setProperty('anchorTop'.toJS, _advancedMarkerAnchorToCssOffset(anchor.dy).toJS);
}

void _copyAdvancedMarkerOptionsAnchor(
gmaps.AdvancedMarkerElement marker,
gmaps.AdvancedMarkerElementOptions options,
) {
final JSAny? anchorLeft = options.getProperty('anchorLeft'.toJS);
if (anchorLeft != null) {
marker.setProperty('anchorLeft'.toJS, anchorLeft);
}

final JSAny? anchorTop = options.getProperty('anchorTop'.toJS);
if (anchorTop != null) {
marker.setProperty('anchorTop'.toJS, anchorTop);
}
}

// Sets the size of the Google Maps icon.
void _setIconSize({required gmaps.Size size, required gmaps.Icon icon}) {
final gmapsSize = gmaps.Size(size.width, size.height);
Expand Down Expand Up @@ -705,6 +734,7 @@ Future<O> _markerOptionsFromMarker<T, O>(Marker marker, T? currentMarker) async
..title = sanitizeHtml(marker.infoWindow.title ?? '')
..zIndex = marker.zIndex
..gmpDraggable = marker.draggable;
_setAdvancedMarkerOptionsAnchor(options, marker.anchor);
return options as O;
} else {
final options = gmaps.MarkerOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ class AdvancedMarkerController
marker.position = options.position;
marker.title = options.title ?? '';
marker.zIndex = options.zIndex;
_copyAdvancedMarkerOptionsAnchor(marker, options);

if (_infoWindow != null && newInfoWindowContent != null) {
_infoWindow.content = newInfoWindowContent;
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.6.2+3
version: 0.6.2+4

environment:
sdk: ^3.10.0
Expand Down