Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[google_maps_flutter] examples - move renderer initialization
  • Loading branch information
jokerttu committed Feb 1, 2023
commit 7e09827e6e3f74a03df2e776bde2eab33213f638
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter_android/google_maps_flutter_android.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
Expand Down Expand Up @@ -77,6 +79,34 @@ void main() {
GoogleMapsFlutterPlatform.instance;
if (mapsImplementation is GoogleMapsFlutterAndroid) {
mapsImplementation.useAndroidViewSurface = true;
initializeMapRenderer();
Copy link
Contributor

Choose a reason for hiding this comment

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

The README should be updated to explain the requirement here, and this should have a comment referring to that to explain why it's here.

Copy link
Contributor Author

@jokerttu jokerttu Jan 31, 2023

Choose a reason for hiding this comment

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

Map renderers are already explained under google_maps_flutter_android package README.
I can update the app-facing plugin README and add information that cloud-based mapid works only on Android if latest renderer is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}
runApp(const MaterialApp(home: MapsDemo()));
}

Completer<AndroidMapRenderer?>? _initializedRendererCompleter;

/// Initializes map renderer to with `latest` renderer type for Android platform.
/// The renderer must be requested before creating GoogleMap instances,
/// as the renderer can be initialized only once per application context.
Future<AndroidMapRenderer?> initializeMapRenderer() async {
if (_initializedRendererCompleter != null) {
return _initializedRendererCompleter!.future;
}

_initializedRendererCompleter = Completer<AndroidMapRenderer?>();

WidgetsFlutterBinding.ensureInitialized();

final GoogleMapsFlutterPlatform mapsImplementation =
GoogleMapsFlutterPlatform.instance;
if (mapsImplementation is GoogleMapsFlutterAndroid) {
mapsImplementation.initializeWithRenderer(AndroidMapRenderer.latest).then(
(AndroidMapRenderer initializedRenderer) =>
_initializedRendererCompleter!.complete(initializedRenderer));
} else {
_initializedRendererCompleter!.complete(null);
}

return _initializedRendererCompleter!.future;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_flutter_android/google_maps_flutter_android.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
import 'main.dart';
import 'page.dart';

class MapIdPage extends GoogleMapExampleAppPage {
Expand Down Expand Up @@ -42,7 +41,7 @@ class MapIdBodyState extends State<MapIdBody> {

@override
void initState() {
_initializeMapRenderer()
initializeMapRenderer()
.then<void>((AndroidMapRenderer? initializedRenderer) => setState(() {
_initializedRenderer = initializedRenderer;
}));
Expand All @@ -56,7 +55,7 @@ class MapIdBodyState extends State<MapIdBody> {
case AndroidMapRenderer.legacy:
return 'legacy';
default:
return 'initializing';
return 'unknown';
}
}

Expand Down Expand Up @@ -142,23 +141,3 @@ class MapIdBodyState extends State<MapIdBody> {
});
}
}

Completer<AndroidMapRenderer?>? _initializedRendererCompleter;
Future<AndroidMapRenderer?> _initializeMapRenderer() async {
if (_initializedRendererCompleter != null) {
return _initializedRendererCompleter!.future;
}

_initializedRendererCompleter = Completer<AndroidMapRenderer?>();
final GoogleMapsFlutterPlatform mapsImplementation =
GoogleMapsFlutterPlatform.instance;
if (mapsImplementation is GoogleMapsFlutterAndroid) {
mapsImplementation.initializeWithRenderer(AndroidMapRenderer.latest).then(
(AndroidMapRenderer initializedRenderer) =>
_initializedRendererCompleter!.complete(initializedRenderer));
} else {
_initializedRendererCompleter!.complete(null);
}

return _initializedRendererCompleter!.future;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter_android/google_maps_flutter_android.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
Expand Down Expand Up @@ -76,5 +78,29 @@ void main() {
final GoogleMapsFlutterPlatform platform = GoogleMapsFlutterPlatform.instance;
// Default to Hybrid Composition for the example.
(platform as GoogleMapsFlutterAndroid).useAndroidViewSurface = true;
initializeMapRenderer();
runApp(const MaterialApp(home: MapsDemo()));
}

Completer<AndroidMapRenderer?>? _initializedRendererCompleter;

/// Initializes map renderer to with `latest` renderer type.
/// The renderer must be requested before creating GoogleMap instances,
/// as the renderer can be initialized only once per application context.
Future<AndroidMapRenderer?> initializeMapRenderer() async {
if (_initializedRendererCompleter != null) {
return _initializedRendererCompleter!.future;
}

_initializedRendererCompleter = Completer<AndroidMapRenderer?>();

WidgetsFlutterBinding.ensureInitialized();

final GoogleMapsFlutterPlatform platform = GoogleMapsFlutterPlatform.instance;
(platform as GoogleMapsFlutterAndroid)
.initializeWithRenderer(AndroidMapRenderer.latest)
.then((AndroidMapRenderer initializedRenderer) =>
_initializedRendererCompleter!.complete(initializedRenderer));

return _initializedRendererCompleter!.future;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

// ignore_for_file: public_member_api_docs

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter_android/google_maps_flutter_android.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';

import 'example_google_map.dart';
import 'main.dart';
import 'page.dart';

class MapIdPage extends GoogleMapExampleAppPage {
Expand Down Expand Up @@ -42,7 +41,7 @@ class MapIdBodyState extends State<MapIdBody> {

@override
void initState() {
_initializeMapRenderer()
initializeMapRenderer()
.then<void>((AndroidMapRenderer? initializedRenderer) => setState(() {
_initializedRenderer = initializedRenderer;
}));
Expand All @@ -56,7 +55,7 @@ class MapIdBodyState extends State<MapIdBody> {
case AndroidMapRenderer.legacy:
return 'legacy';
default:
return 'initializing';
return 'unknown';
}
}

Expand Down Expand Up @@ -134,25 +133,3 @@ class MapIdBodyState extends State<MapIdBody> {
});
}
}

Completer<AndroidMapRenderer?>? _initializedRendererCompleter;
Future<AndroidMapRenderer?> _initializeMapRenderer() async {
// Map renderer can be requested only once.
// Returns existing copleter future for serial requests.
if (_initializedRendererCompleter != null) {
return _initializedRendererCompleter!.future;
}

_initializedRendererCompleter = Completer<AndroidMapRenderer?>();
final GoogleMapsFlutterPlatform mapsImplementation =
GoogleMapsFlutterPlatform.instance;
if (mapsImplementation is GoogleMapsFlutterAndroid) {
mapsImplementation.initializeWithRenderer(AndroidMapRenderer.latest).then(
(AndroidMapRenderer initializedRenderer) =>
_initializedRendererCompleter!.complete(initializedRenderer));
} else {
_initializedRendererCompleter!.complete(null);
}

return _initializedRendererCompleter!.future;
}