Skip to content

Commit e2aa2ea

Browse files
authored
[google_maps_flutter_web] Avoid replacing advanced marker content on move (flutter#11952)
## Description Avoid replacing an `AdvancedMarkerElement`'s DOM content when a marker update changes only its position. Recreating byte-backed bitmap content produces a new blob-backed image node, which causes the marker to disappear or flicker while moving. The web implementation now tracks the marker properties that affect rendered content (icon configuration, alpha, visibility, and rotation). Position-only updates preserve the existing DOM node; visual changes still replace it. Fixes flutter/flutter#188291. ## Tests - Added web integration coverage for repeated position updates using both `BitmapDescriptor.bytes` and `BytesMapBitmap`. - Added coverage that visual configuration changes replace the content. - Ran formatting, analysis, Dart tests, package validation, and publish checks for `google_maps_flutter_web`. - Ran `advanced_markers_test.dart` through `flutter drive` in Chrome. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 65e5650 commit e2aa2ea

6 files changed

Lines changed: 214 additions & 14 deletions

File tree

‎packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.3+1
2+
3+
* Fixes flickering of byte-backed advanced markers when updating their position on web.
4+
15
## 0.6.3
26

37
* Adds support for mapTypeControlEnabled, fullscreenControlEnabled, and streetViewControlEnabled.

‎packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/advanced_markers_test.dart‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,109 @@ void main() {
335335
expect(icon.style.height, '${expectedSize}px');
336336
});
337337

338+
for (final descriptorFactory in <(String, BitmapDescriptor Function(Uint8List))>[
339+
(
340+
'BitmapDescriptor.bytes',
341+
(Uint8List bytes) => BitmapDescriptor.bytes(bytes, imagePixelRatio: 1),
342+
),
343+
('BytesMapBitmap', (Uint8List bytes) => BytesMapBitmap(bytes, imagePixelRatio: 1)),
344+
]) {
345+
testWidgets(
346+
'changeMarkers preserves ${descriptorFactory.$1} content across position changes',
347+
(WidgetTester tester) async {
348+
const markerId = MarkerId('1');
349+
final Uint8List bytes = const Base64Decoder().convert(iconImageBase64);
350+
await controller.addMarkers(<AdvancedMarker>{
351+
AdvancedMarker(
352+
markerId: markerId,
353+
icon: descriptorFactory.$2(bytes),
354+
position: const LatLng(1, 2),
355+
),
356+
});
357+
358+
final gmaps.AdvancedMarkerElement? marker = controller.markers[markerId]?.marker;
359+
expect(marker, isNotNull);
360+
final icon = marker!.content as HTMLImageElement?;
361+
expect(icon, isNotNull);
362+
final String blobUrl = icon!.src;
363+
364+
for (final updatedPosition in <LatLng>[
365+
const LatLng(3, 4),
366+
const LatLng(5, 6),
367+
const LatLng(42, 54),
368+
]) {
369+
await controller.changeMarkers(<AdvancedMarker>{
370+
AdvancedMarker(
371+
markerId: markerId,
372+
icon: descriptorFactory.$2(bytes),
373+
position: updatedPosition,
374+
),
375+
});
376+
377+
final position = marker.position! as gmaps.LatLngLiteral;
378+
expect(position.lat, updatedPosition.latitude);
379+
expect(position.lng, updatedPosition.longitude);
380+
expect(icon.isSameNode(marker.content), isTrue);
381+
expect(icon.src, blobUrl);
382+
}
383+
},
384+
);
385+
}
386+
387+
testWidgets('changeMarkers replaces bitmap icon content when alpha changes', (
388+
WidgetTester tester,
389+
) async {
390+
const markerId = MarkerId('1');
391+
final Uint8List bytes = const Base64Decoder().convert(iconImageBase64);
392+
final markers = <AdvancedMarker>{
393+
AdvancedMarker(markerId: markerId, icon: BytesMapBitmap(bytes, imagePixelRatio: 1)),
394+
};
395+
await controller.addMarkers(markers);
396+
397+
final gmaps.AdvancedMarkerElement? marker = controller.markers[markerId]?.marker;
398+
expect(marker, isNotNull);
399+
final icon = marker!.content as HTMLImageElement?;
400+
expect(icon, isNotNull);
401+
402+
final updatedMarkers = <AdvancedMarker>{
403+
AdvancedMarker(
404+
markerId: markerId,
405+
alpha: 0.5,
406+
icon: BytesMapBitmap(bytes, imagePixelRatio: 1),
407+
),
408+
};
409+
await controller.changeMarkers(updatedMarkers);
410+
411+
final updatedIcon = marker.content as HTMLImageElement?;
412+
expect(updatedIcon, isNotNull);
413+
expect(updatedIcon!.isSameNode(icon), isFalse);
414+
expect(updatedIcon.style.opacity, '0.5');
415+
});
416+
417+
testWidgets('changeMarkers replaces content when the bitmap configuration changes', (
418+
WidgetTester tester,
419+
) async {
420+
const markerId = MarkerId('1');
421+
final Uint8List bytes = const Base64Decoder().convert(iconImageBase64);
422+
await controller.addMarkers(<AdvancedMarker>{
423+
AdvancedMarker(markerId: markerId, icon: BytesMapBitmap(bytes, width: 16)),
424+
});
425+
426+
final gmaps.AdvancedMarkerElement? marker = controller.markers[markerId]?.marker;
427+
expect(marker, isNotNull);
428+
final icon = marker!.content as HTMLImageElement?;
429+
expect(icon, isNotNull);
430+
431+
await controller.changeMarkers(<AdvancedMarker>{
432+
AdvancedMarker(markerId: markerId, icon: BytesMapBitmap(bytes, width: 20)),
433+
});
434+
435+
final updatedIcon = marker.content as HTMLImageElement?;
436+
expect(updatedIcon, isNotNull);
437+
expect(updatedIcon!.isSameNode(icon), isFalse);
438+
expect(updatedIcon.style.width, '20px');
439+
});
440+
338441
testWidgets('markers with custom bitmap icon and pixel ratio work', (
339442
WidgetTester tester,
340443
) async {

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

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -688,22 +688,72 @@ Future<gmaps.Icon?> _gmIconFromBitmapDescriptor(
688688
return icon;
689689
}
690690

691+
class _AdvancedMarkerContentConfiguration {
692+
_AdvancedMarkerContentConfiguration({
693+
required this.alpha,
694+
required this.visible,
695+
required this.rotation,
696+
required this.icon,
697+
required this.iconConfiguration,
698+
});
699+
700+
factory _AdvancedMarkerContentConfiguration.fromMarker(AdvancedMarker marker) {
701+
return _AdvancedMarkerContentConfiguration(
702+
alpha: marker.alpha,
703+
visible: marker.visible,
704+
rotation: marker.rotation,
705+
icon: marker.icon,
706+
iconConfiguration: marker.icon.toJson(),
707+
);
708+
}
709+
710+
final double alpha;
711+
final bool visible;
712+
final double rotation;
713+
final BitmapDescriptor icon;
714+
final Object iconConfiguration;
715+
716+
bool isMatchFor(AdvancedMarker marker) {
717+
return alpha == marker.alpha &&
718+
visible == marker.visible &&
719+
rotation == marker.rotation &&
720+
(identical(icon, marker.icon) ||
721+
const DeepCollectionEquality().equals(iconConfiguration, marker.icon.toJson()));
722+
}
723+
}
724+
725+
bool _isAdvancedMarkerContentUpdateRequired(
726+
Marker marker,
727+
_AdvancedMarkerContentConfiguration? previousConfiguration,
728+
) {
729+
if (marker is! AdvancedMarker || previousConfiguration == null) {
730+
return true;
731+
}
732+
return !previousConfiguration.isMatchFor(marker);
733+
}
734+
691735
// Computes the options for a new [gmaps.Marker] from an incoming set of options
692736
// [marker], and the existing marker registered with the map: [currentMarker].
693-
Future<O> _markerOptionsFromMarker<T, O>(Marker marker, T? currentMarker) async {
737+
Future<O> _markerOptionsFromMarker<T, O>(
738+
Marker marker,
739+
T? currentMarker, {
740+
bool isAdvancedMarkerContentUpdateRequired = true,
741+
}) async {
694742
if (marker is AdvancedMarker) {
695743
final options = gmaps.AdvancedMarkerElementOptions()
696744
..collisionBehavior = _markerCollisionBehaviorToGmCollisionBehavior(marker.collisionBehavior)
697-
..content = await _advancedMarkerIconFromBitmapDescriptor(
698-
marker.icon,
699-
opacity: marker.alpha,
700-
isVisible: marker.visible,
701-
rotation: marker.rotation,
702-
)
703745
..position = gmaps.LatLng(marker.position.latitude, marker.position.longitude)
704746
..title = sanitizeHtml(marker.infoWindow.title ?? '')
705747
..zIndex = marker.zIndex
706748
..gmpDraggable = marker.draggable;
749+
if (isAdvancedMarkerContentUpdateRequired) {
750+
options.content = await _advancedMarkerIconFromBitmapDescriptor(
751+
marker.icon,
752+
opacity: marker.alpha,
753+
isVisible: marker.visible,
754+
rotation: marker.rotation,
755+
);
756+
}
707757
return options as O;
708758
} else {
709759
final options = gmaps.MarkerOptions()

‎packages/google_maps_flutter/google_maps_flutter_web/lib/src/marker.dart‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,21 @@ class AdvancedMarkerController
303303

304304
@override
305305
void update(gmaps.AdvancedMarkerElementOptions options, {web.HTMLElement? newInfoWindowContent}) {
306+
_update(options, isContentUpdateRequired: true, newInfoWindowContent: newInfoWindowContent);
307+
}
308+
309+
void _update(
310+
gmaps.AdvancedMarkerElementOptions options, {
311+
required bool isContentUpdateRequired,
312+
web.HTMLElement? newInfoWindowContent,
313+
}) {
306314
assert(_marker != null, 'Cannot `update` Marker after calling `remove`.');
307315

308316
final gmaps.AdvancedMarkerElement marker = _marker!;
309317
marker.collisionBehavior = options.collisionBehavior;
310-
marker.content = options.content;
318+
if (isContentUpdateRequired) {
319+
marker.content = options.content;
320+
}
311321
marker.gmpDraggable = options.gmpDraggable;
312322
marker.position = options.position;
313323
marker.title = options.title ?? '';

‎packages/google_maps_flutter/google_maps_flutter_web/lib/src/markers.dart‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ abstract class MarkersController<T extends Object, O> extends GeometryController
3030
// A cache of [MarkerController]s indexed by their [MarkerId].
3131
final Map<MarkerId, MarkerController<T, O>> _markerIdToController;
3232

33+
// A cache of the advanced marker content configuration indexed by [MarkerId].
34+
final Map<MarkerId, _AdvancedMarkerContentConfiguration> _contentConfigurationByMarkerId =
35+
<MarkerId, _AdvancedMarkerContentConfiguration>{};
36+
3337
// The stream over which markers broadcast their events
3438
final StreamController<MapEvent<Object?>> _streamController;
3539

@@ -103,6 +107,7 @@ abstract class MarkersController<T extends Object, O> extends GeometryController
103107
gmInfoWindow,
104108
);
105109
_markerIdToController[marker.markerId] = controller;
110+
_updateAdvancedMarkerContentConfiguration(marker);
106111

107112
return controller;
108113
}
@@ -136,16 +141,42 @@ abstract class MarkersController<T extends Object, O> extends GeometryController
136141
_removeMarker(marker.markerId);
137142
await _addMarker(marker);
138143
} else {
139-
final O markerOptions = await _markerOptionsFromMarker(marker, markerController.marker);
140-
final gmaps.InfoWindowOptions? infoWindow = _infoWindowOptionsFromMarker(marker);
141-
markerController.update(
142-
markerOptions,
143-
newInfoWindowContent: infoWindow?.content as web.HTMLElement?,
144+
final _AdvancedMarkerContentConfiguration? previousContentConfiguration =
145+
_contentConfigurationByMarkerId[marker.markerId];
146+
final bool isAdvancedMarkerContentUpdateRequired = _isAdvancedMarkerContentUpdateRequired(
147+
marker,
148+
previousContentConfiguration,
149+
);
150+
final O markerOptions = await _markerOptionsFromMarker(
151+
marker,
152+
markerController.marker,
153+
isAdvancedMarkerContentUpdateRequired: isAdvancedMarkerContentUpdateRequired,
144154
);
155+
final gmaps.InfoWindowOptions? infoWindow = _infoWindowOptionsFromMarker(marker);
156+
final infoWindowContent = infoWindow?.content as web.HTMLElement?;
157+
if (markerController is AdvancedMarkerController) {
158+
(markerController as AdvancedMarkerController)._update(
159+
markerOptions as gmaps.AdvancedMarkerElementOptions,
160+
isContentUpdateRequired: isAdvancedMarkerContentUpdateRequired,
161+
newInfoWindowContent: infoWindowContent,
162+
);
163+
} else {
164+
markerController.update(markerOptions, newInfoWindowContent: infoWindowContent);
165+
}
166+
_updateAdvancedMarkerContentConfiguration(marker);
145167
}
146168
}
147169
}
148170

171+
void _updateAdvancedMarkerContentConfiguration(Marker marker) {
172+
if (marker is AdvancedMarker) {
173+
_contentConfigurationByMarkerId[marker.markerId] =
174+
_AdvancedMarkerContentConfiguration.fromMarker(marker);
175+
} else {
176+
_contentConfigurationByMarkerId.remove(marker.markerId);
177+
}
178+
}
179+
149180
/// Removes a set of [MarkerId]s from the cache.
150181
void removeMarkers(Set<MarkerId> markerIdsToRemove) {
151182
final List<MapEntry<MarkerId, MarkerController<T, O>?>> markersControllers = markerIdsToRemove
@@ -182,6 +213,7 @@ abstract class MarkersController<T extends Object, O> extends GeometryController
182213
for (final markerController in markersControllers) {
183214
markerController.value?.remove();
184215
_markerIdToController.remove(markerController.key);
216+
_contentConfigurationByMarkerId.remove(markerController.key);
185217
}
186218
}
187219

@@ -195,6 +227,7 @@ abstract class MarkersController<T extends Object, O> extends GeometryController
195227
}
196228
markerController?.remove();
197229
_markerIdToController.remove(markerId);
230+
_contentConfigurationByMarkerId.remove(markerId);
198231
}
199232

200233
// InfoWindow...

‎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.6.3
5+
version: 0.6.3+1
66

77
environment:
88
sdk: ^3.10.0

0 commit comments

Comments
 (0)