Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 880e0c5

Browse files
committed
[google_maps_flutter_android] Add support to prefer renderer
1 parent cb76ba5 commit 880e0c5

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

packages/google_maps_flutter/google_maps_flutter_android/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.4.0
2+
3+
* Adds the ability to set the preferred renderer with a
4+
metadata key in the AndroidManifest file.
5+
16
## 2.3.2
27

38
* Update `com.google.android.gms:play-services-maps` to 18.1.0.

packages/google_maps_flutter/google_maps_flutter_android/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,24 @@ Hybrid Composition, but currently [misses certain map updates][4].
4848
This mode will likely become the default in future versions if/when the
4949
missed updates issue can be resolved.
5050

51+
## Map renderer
52+
53+
This plugin supports the option of choosing a preferred [map renderer][5].
54+
55+
Prefer renderer in the application manifest `android/app/src/main/AndroidManifest.xml`:
56+
57+
```xml
58+
<manifest ...
59+
<application ...
60+
<meta-data
61+
android:name="io.flutter.plugins.googlemaps.PREFERRED_RENDERER"
62+
android:value="latest" />
63+
```
64+
Available values are `latest` and `legacy`.
65+
66+
5167
[1]: https://pub.dev/packages/google_maps_flutter
5268
[2]: https://flutter.dev/docs/development/packages-and-plugins/developing-packages#endorsed-federated-plugin
5369
[3]: https://docs.flutter.dev/development/platform-integration/android/platform-views
5470
[4]: https://github.com/flutter/flutter/issues/103686
71+
[5]: https://developers.google.com/maps/documentation/android-sdk/renderer

packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.Manifest;
88
import android.annotation.SuppressLint;
99
import android.content.Context;
10+
import android.content.pm.ApplicationInfo;
1011
import android.content.pm.PackageManager;
1112
import android.graphics.Bitmap;
1213
import android.graphics.Point;
@@ -25,6 +26,8 @@
2526
import com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback;
2627
import com.google.android.gms.maps.GoogleMapOptions;
2728
import com.google.android.gms.maps.MapView;
29+
import com.google.android.gms.maps.MapsInitializer;
30+
import com.google.android.gms.maps.MapsInitializer.Renderer;
2831
import com.google.android.gms.maps.OnMapReadyCallback;
2932
import com.google.android.gms.maps.model.CameraPosition;
3033
import com.google.android.gms.maps.model.Circle;
@@ -57,6 +60,7 @@ final class GoogleMapController
5760
PlatformView {
5861

5962
private static final String TAG = "GoogleMapController";
63+
private static boolean preferredRendererInitialized = false;
6064
private final int id;
6165
private final MethodChannel methodChannel;
6266
private final GoogleMapOptions options;
@@ -94,6 +98,9 @@ final class GoogleMapController
9498
this.id = id;
9599
this.context = context;
96100
this.options = options;
101+
102+
this.initializePreferredRenderer(context);
103+
97104
this.mapView = new MapView(context, options);
98105
this.density = context.getResources().getDisplayMetrics().density;
99106
methodChannel =
@@ -175,6 +182,42 @@ public void onMapLoaded() {
175182
});
176183
}
177184

185+
/**
186+
* Initializes map renderer if meta-data value for key
187+
* "io.flutter.plugins.googlemaps.PREFERRED_RENDERER" is set in AndroidManifest. Renderer can be
188+
* initialized only once per application context.
189+
*
190+
* <p>Supported meta-data values are "legacy" and "latest".
191+
*/
192+
private void initializePreferredRenderer(Context context) {
193+
if (!preferredRendererInitialized) {
194+
try {
195+
ApplicationInfo applicationInfo =
196+
context
197+
.getPackageManager()
198+
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
199+
200+
if (applicationInfo != null && applicationInfo.metaData != null) {
201+
final String preferredRenderer =
202+
applicationInfo.metaData.getString(
203+
"io.flutter.plugins.googlemaps.PREFERRED_RENDERER");
204+
205+
if (preferredRenderer != null) {
206+
switch (preferredRenderer) {
207+
case "latest":
208+
MapsInitializer.initialize(context, Renderer.LATEST, null);
209+
case "legacy":
210+
MapsInitializer.initialize(context, Renderer.LEGACY, null);
211+
}
212+
}
213+
}
214+
} catch (Exception e) {
215+
e.printStackTrace();
216+
}
217+
preferredRendererInitialized = true;
218+
}
219+
}
220+
178221
private static void postFrameCallback(Runnable f) {
179222
Choreographer.getInstance()
180223
.postFrameCallback(
@@ -848,6 +891,11 @@ public void setInitialTileOverlays(List<Map<String, ?>> initialTileOverlays) {
848891
}
849892
}
850893

894+
@Override
895+
public void setMapId(String mapId) {
896+
Log.e(TAG, "Cannot change MapId after map is initialized.");
897+
}
898+
851899
private void updateInitialTileOverlays() {
852900
tileOverlaysController.addTileOverlays(initialTileOverlays);
853901
}

packages/google_maps_flutter/google_maps_flutter_android/example/android/app/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ android {
4242
}
4343

4444
defaultConfig {
45-
manifestPlaceholders = [mapsApiKey: "$System.env.MAPS_API_KEY"]
45+
manifestPlaceholders = [
46+
mapsApiKey: "$System.env.MAPS_API_KEY",
47+
mapsPreferredRenderer: "$System.env.MAPS_PREFERRED_RENDERER"
48+
]
4649
}
4750

4851
buildTypes {

packages/google_maps_flutter/google_maps_flutter_android/example/android/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<meta-data
1313
android:name="com.google.android.geo.API_KEY"
1414
android:value="${mapsApiKey}" />
15+
<meta-data
16+
android:name="io.flutter.plugins.googlemaps.PREFERRED_RENDERER"
17+
android:value="${mapsPreferredRenderer}" />
1518
<activity android:name="io.flutter.embedding.android.FlutterActivity"
1619
android:theme="@style/LaunchTheme"
1720
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"

0 commit comments

Comments
 (0)