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

Commit e5cb1a5

Browse files
authored
Port google_maps_flutter to use AndroidView for embedding the map. (#743)
This change adds a GoogleMap widget which shows a map as part of the widget tree. Each map uses a dedicated method channel named 'plugins.flutter.io/google_maps_$id', The Java GoogleMapsController class now extends PlatformView, and is the Java endpoint of the per map method channel. This change disables the plugin on iOS until we have an inlining solution for iOS.
1 parent 69380a9 commit e5cb1a5

File tree

15 files changed

+485
-988
lines changed

15 files changed

+485
-988
lines changed

packages/google_maps_flutter/README.md

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
[![pub package](https://img.shields.io/pub/v/google_maps_flutter.svg)](https://pub.dartlang.org/packages/google_maps_flutter)
44

5-
A Flutter plugin to use [Google Maps](https://developers.google.com/maps/) in
6-
iOS and Android apps.
5+
A Flutter plugin to use [Google Maps](https://developers.google.com/maps/).
76

87
## Caveat
98

@@ -12,24 +11,9 @@ This plugin provides an *unpublished preview* of the Flutter API for Google Maps
1211
code are still being consolidated and expanded. The intention is to grow
1312
current coverage into a complete offering. Issues and pull requests aimed to
1413
help us prioritize and speed up this effort are very welcome.
15-
* The technique currently used for compositing GoogleMap views with Flutter
16-
widgets is *inherently limited* and will be replaced by a fully compositional
17-
[Texture](https://docs.flutter.io/flutter/widgets/Texture-class.html)-based
18-
approach before we publish this plugin.
19-
20-
In detail: the plugin currently relies on placing platform overlays on top of
21-
a bitmap snapshotting widget for creating the illusion of in-line compositing
22-
of GoogleMap views with Flutter widgets. This works only in very limited
23-
situations where
24-
* the widget is stationary
25-
* the widget is drawn on top of all other widgets within bounds
26-
* touch events within widget bounds can be safely ignored by Flutter
27-
28-
The root problem with platform overlays is that they cannot be freely composed
29-
with other widgets. Many workarounds can be devised to address this shortcoming
30-
in particular situations, but the Flutter team does not intend to support such
31-
work, as it would not move us forward towards our goal of a fully compositional
32-
GoogleMaps widget.
14+
* Currently the plugin only supports Android as it embeds a platform view in the
15+
Flutter hierarchy which is currently only supported for Android ([tracking
16+
issue](https://github.com/flutter/flutter/issues/19030)).
3317

3418
## Usage
3519

@@ -79,38 +63,32 @@ Supply your API key in the application delegate `ios/Runner/AppDelegate.m`:
7963

8064
### Both
8165

82-
You can now instantiate a `GoogleMapOverlayController` and use it to configure
83-
a `GoogleMapOverlay` widget. Client code will have to change once the plugin
84-
stops using platform overlays.
66+
You can now add a `GoogleMap` widget to your widget tree.
8567

86-
Once added as an overlay, the map view can be controlled via the
87-
`GoogleMapController` that you obtain as the `mapController` property of
88-
the overlay controller. Client code written against the `GoogleMapController`
89-
interface will be unaffected by the change away from platform overlays.
68+
The map view can be controlled with the `GoogleMapController` that is passed to
69+
the `GoogleMap`'s `onMapCreated` callback.
9070

9171
```dart
9272
import 'package:flutter/material.dart';
9373
import 'package:google_maps_flutter/google_maps_flutter.dart';
9474
9575
void main() {
96-
GoogleMapController.init();
97-
final GoogleMapOverlayController controller =
98-
GoogleMapOverlayController.fromSize(width: 300.0, height: 200.0);
99-
final Widget mapWidget = GoogleMapOverlay(controller: controller);
10076
runApp(MaterialApp(
10177
home: new Scaffold(
10278
appBar: AppBar(title: const Text('Google Maps demo')),
103-
body: MapsDemo(mapWidget, controller.mapController),
79+
body: MapsDemo(),
10480
),
105-
navigatorObservers: <NavigatorObserver>[controller.overlayController],
10681
));
10782
}
10883
109-
class MapsDemo extends StatelessWidget {
110-
MapsDemo(this.mapWidget, this.controller);
84+
class MapsDemo extends StatefulWidget {
85+
@override
86+
State createState() => MapsDemoState();
87+
}
88+
89+
class MapsDemoState extends State<MapsDemo> {
11190
112-
final Widget mapWidget;
113-
final GoogleMapController controller;
91+
GoogleMapController mapController;
11492
11593
@override
11694
Widget build(BuildContext context) {
@@ -119,11 +97,19 @@ class MapsDemo extends StatelessWidget {
11997
child: Column(
12098
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
12199
children: <Widget>[
122-
Center(child: mapWidget),
100+
Center(
101+
child: SizedBox(
102+
width: 300.0,
103+
height: 200.0,
104+
child: GoogleMap(
105+
onMapCreated: _onMapCreated,
106+
),
107+
),
108+
),
123109
RaisedButton(
124110
child: const Text('Go to London'),
125-
onPressed: () {
126-
controller.animateCamera(CameraUpdate.newCameraPosition(
111+
onPressed: mapController == null ? null : () {
112+
mapController.animateCamera(CameraUpdate.newCameraPosition(
127113
const CameraPosition(
128114
bearing: 270.0,
129115
target: LatLng(51.5160895, -0.1294527),
@@ -137,6 +123,10 @@ class MapsDemo extends StatelessWidget {
137123
),
138124
);
139125
}
126+
127+
void _onMapCreated(GoogleMapController controller) {
128+
setState(() { mapController = controller; });
129+
}
140130
}
141131
```
142132

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
package io.flutter.plugins.googlemaps;
66

7+
import android.content.Context;
78
import com.google.android.gms.maps.GoogleMapOptions;
89
import com.google.android.gms.maps.model.CameraPosition;
910
import com.google.android.gms.maps.model.LatLngBounds;
10-
import io.flutter.plugin.common.MethodChannel;
1111
import io.flutter.plugin.common.PluginRegistry;
1212
import java.util.concurrent.atomic.AtomicInteger;
1313

@@ -16,13 +16,9 @@ class GoogleMapBuilder implements GoogleMapOptionsSink {
1616
private boolean trackCameraPosition = false;
1717

1818
GoogleMapController build(
19-
AtomicInteger state,
20-
PluginRegistry.Registrar registrar,
21-
int width,
22-
int height,
23-
MethodChannel.Result result) {
19+
int id, Context context, AtomicInteger state, PluginRegistry.Registrar registrar) {
2420
final GoogleMapController controller =
25-
new GoogleMapController(state, registrar, width, height, options, result);
21+
new GoogleMapController(id, context, state, registrar, options);
2622
controller.init();
2723
controller.setTrackCameraPosition(trackCameraPosition);
2824
return controller;

0 commit comments

Comments
 (0)