From 13a2e047d474e8653bc818d14a405faa445bc199 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Wed, 18 May 2022 17:59:06 -0700 Subject: [PATCH] Fix issue where markers aren't updated in Flutter v3.0.0 --- .../google_maps_flutter/CHANGELOG.md | 3 +- .../googlemaps/GoogleMapController.java | 33 +++++++++++++++++++ .../google_maps_flutter/pubspec.yaml | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md index 57d24e2196a5..c342e3af8be7 100644 --- a/packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md +++ b/packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md @@ -1,5 +1,6 @@ -## NEXT +## 2.1.6 +* Fixes issue in Flutter v3.0.0 where markers aren't updated. * Fixes iOS native unit tests on M1 devices. * Minor fixes for new analysis options. diff --git a/packages/google_maps_flutter/google_maps_flutter/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java b/packages/google_maps_flutter/google_maps_flutter/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java index 9b8810354b8f..8c504d93c368 100644 --- a/packages/google_maps_flutter/google_maps_flutter/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java +++ b/packages/google_maps_flutter/google_maps_flutter/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java @@ -12,6 +12,7 @@ import android.graphics.Point; import android.os.Bundle; import android.util.Log; +import android.view.Choreographer; import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -151,6 +152,17 @@ public void onMapReady(GoogleMap googleMap) { updateInitialTileOverlays(); } + private static void postFrameCallback(Runnable f) { + Choreographer.getInstance().postFrameCallback( + new Choreographer.FrameCallback() { + @Override + public void doFrame(long frameTimeNanos) { + f.run(); + } + } + ); + } + @Override public void onMethodCall(MethodCall call, MethodChannel.Result result) { switch (call.method) { @@ -250,6 +262,27 @@ public void onSnapshotReady(Bitmap bitmap) { markersController.changeMarkers(markersToChange); List markerIdsToRemove = call.argument("markerIdsToRemove"); markersController.removeMarkers(markerIdsToRemove); + + // gmscore GL renderer uses a TextureView. + // Android platform views that are displayed as a texture after Flutter v3.0.0. + // require that the view hierarchy is notified after all drawing operations have been flushed. + // Since the GL renderer doesn't use standard Android views, and instead uses GL directly, + // we notify the view hierarchy by invalidating the view. + // Unfortunately, when OnMapLoadedCallback is fired, the texture may not have been updated yet. + // To workaround this limitation, wait two frames. + // This ensures that at least the frame budget (16.66ms at 60hz) have passed since the + // drawing operation was issued. + googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() { + @Override + public void onMapLoaded() { + postFrameCallback(() -> { + postFrameCallback(() -> { + mapView.invalidate(); + }); + }); + } + }); + result.success(null); break; } diff --git a/packages/google_maps_flutter/google_maps_flutter/pubspec.yaml b/packages/google_maps_flutter/google_maps_flutter/pubspec.yaml index a294dd09981f..7a8d60e46362 100644 --- a/packages/google_maps_flutter/google_maps_flutter/pubspec.yaml +++ b/packages/google_maps_flutter/google_maps_flutter/pubspec.yaml @@ -2,7 +2,7 @@ name: google_maps_flutter description: A Flutter plugin for integrating Google Maps in iOS and Android applications. repository: https://github.com/flutter/plugins/tree/main/packages/google_maps_flutter/google_maps_flutter issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22 -version: 2.1.5 +version: 2.1.6 environment: sdk: ">=2.14.0 <3.0.0"