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

[google_maps_flutter] Maps snapshot #1719

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
50d99f8
[google_maps_flutter] snapshot on android
duzenko Jun 10, 2019
9cb8757
iOS side + formatting
duzenko Jun 10, 2019
51a499e
Merge branch 'master' into maps-snapshot
duzenko Jun 10, 2019
d4a7b4c
Post merge fixes
duzenko Jun 10, 2019
e30a8b6
Format
duzenko Jun 10, 2019
d661056
Analizer appeased
duzenko Jun 11, 2019
346dee6
Merge branch 'maps-snapshot' of https://github.com/duzenko/plugins in…
duzenko Jun 11, 2019
fb894e8
Merge remote-tracking branch 'upstream/master' into maps-snapshot
duzenko Jun 11, 2019
0b9963b
Merge remote-tracking branch 'upstream/master' into maps-snapshot
duzenko Jun 11, 2019
b7bd02b
Merge branch 'maps-snapshot' of https://github.com/duzenko/plugins in…
duzenko Jun 11, 2019
c04d235
Check failure blind shooting
duzenko Jun 11, 2019
cfe1f59
Blind shooting
duzenko Jun 11, 2019
cc16b6d
Reinstate my changes as the checking is seemingly broken
duzenko Jun 11, 2019
63a9b25
Merge commit '1e01c9e12d564642735e1a7308a21083b9769b11' into maps-sna…
duzenko Jun 13, 2019
ab7d53f
Merge remote-tracking branch 'upstream/master' into maps-snapshot
duzenko Jun 20, 2019
40148ca
Integration test. Bugfix.
duzenko Jun 20, 2019
5b0060a
Formatting
duzenko Jun 20, 2019
ef97946
Muckup revert
duzenko Jun 21, 2019
945e5d5
Merge commit '400bc414afb1e3088ff3c59b841149d1fba1a8e1' into maps-sna…
duzenko Jun 21, 2019
58afa69
iOS scaling fix
duzenko Jun 21, 2019
ec51343
Update main.dart
duzenko Jan 23, 2020
e0df524
Merge branch 'upstream-master' into maps-snapshot
duzenko Jan 24, 2020
ecc1560
formatting
Jan 28, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
Expand All @@ -37,6 +38,7 @@
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.platform.PlatformView;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -60,6 +62,7 @@ final class GoogleMapController
OnMapReadyCallback,
GoogleMap.OnMapClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.SnapshotReadyCallback,
PlatformView {

private static final String TAG = "GoogleMapController";
Expand Down Expand Up @@ -220,6 +223,12 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
}
break;
}
case "map#snapshot":
{
googleMap.snapshot(this);
result.success(null);
break;
}
case "camera#move":
{
final CameraUpdate cameraUpdate =
Expand Down Expand Up @@ -403,6 +412,15 @@ public void onCircleClick(Circle circle) {
circlesController.onCircleTap(circle.getId());
}

@Override
public void onSnapshotReady(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bitmap.recycle();
methodChannel.invokeMethod("map#onSnapshot", byteArray);
}

@Override
public void dispose() {
if (disposed) {
Expand Down
8 changes: 4 additions & 4 deletions packages/google_maps_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class MapsDemo extends StatelessWidget {
body: ListView.builder(
itemCount: _allPages.length,
itemBuilder: (_, int index) => ListTile(
leading: _allPages[index].leading,
title: Text(_allPages[index].title),
onTap: () => _pushPage(context, _allPages[index]),
),
leading: _allPages[index].leading,
title: Text(_allPages[index].title),
onTap: () => _pushPage(context, _allPages[index]),
),
duzenko marked this conversation as resolved.
Show resolved Hide resolved
),
);
}
Expand Down
39 changes: 39 additions & 0 deletions packages/google_maps_flutter/example/lib/map_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter/services.dart' show rootBundle;
Expand Down Expand Up @@ -186,6 +189,14 @@ class MapUiBodyState extends State<MapUiBody> {
);
}

Widget _snapshotter() {
return FlatButton(
child: const Text('make snapshot'),
onPressed: () {
_controller.snapshot();
});
}

Future<String> _getFileData(String path) async {
return await rootBundle.loadString(path);
}
Expand Down Expand Up @@ -232,6 +243,7 @@ class MapUiBodyState extends State<MapUiBody> {
myLocationEnabled: _myLocationEnabled,
myLocationButtonEnabled: _myLocationButtonEnabled,
onCameraMove: _updateCameraPosition,
onSnapshot: _snapshot,
);

final List<Widget> columnChildren = <Widget>[
Expand Down Expand Up @@ -269,6 +281,7 @@ class MapUiBodyState extends State<MapUiBody> {
_zoomToggler(),
_myLocationToggler(),
_myLocationButtonToggler(),
_snapshotter(),
_nightModeToggler(),
],
),
Expand All @@ -289,9 +302,35 @@ class MapUiBodyState extends State<MapUiBody> {
}

void onMapCreated(GoogleMapController controller) {
_controller = controller;
duzenko marked this conversation as resolved.
Show resolved Hide resolved
setState(() {
_controller = controller;
_isMapCreated = true;
});
}

void _snapshot(Uint8List argument) async {
duzenko marked this conversation as resolved.
Show resolved Hide resolved
final ui.Codec codec = await ui.instantiateImageCodec(argument);
final ui.FrameInfo frame = await codec.getNextFrame();
final ui.Image img = frame.image;
print('${argument.length} bytes, ${img.width} x ${img.height} px');
await showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
actions: <Widget>[
FlatButton(
child: const Text('OK'),
onPressed: () => Navigator.of(context).pop(),
)
],
content: Padding(
padding: const EdgeInsets.symmetric(vertical: 66),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[Image.memory(argument)],
)));
},
);
}
}
32 changes: 32 additions & 0 deletions packages/google_maps_flutter/example/test_driver/google_maps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/widgets.dart';
import 'package:flutter_driver/driver_extension.dart';
Expand Down Expand Up @@ -498,4 +500,34 @@ void main() {
final GoogleMapController controller = await controllerCompleter.future;
await controller.setMapStyle(null);
});

test('testSnapshot', () async {
final GlobalKey key = GlobalKey();
final Completer<GoogleMapController> controllerCompleter =
Completer<GoogleMapController>();
final Completer<Uint8List> snapshotCompleter = Completer<Uint8List>();

await pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
key: key,
initialCameraPosition: _kInitialCameraPosition,
onMapCreated: (GoogleMapController controller) {
controllerCompleter.complete(controller);
},
onSnapshot: (Uint8List data) {
snapshotCompleter.complete(data);
},
),
));

final GoogleMapController controller = await controllerCompleter.future;
await controller.snapshot();
final Uint8List data = await snapshotCompleter.future;
final ui.Codec codec = await ui.instantiateImageCodec(data);
final ui.FrameInfo frame = await codec.getNextFrame();
final ui.Image img = frame.image;
final RenderBox box = key.currentContext.findRenderObject();
expect(ui.window.devicePixelRatio * box.size.height, img.height);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ - (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
} else if ([call.method isEqualToString:@"map#update"]) {
InterpretMapOptions(call.arguments[@"options"], self);
result(PositionToJson([self cameraPosition]));
} else if ([call.method isEqualToString:@"map#snapshot"]) {
UIGraphicsBeginImageContextWithOptions(_mapView.frame.size, NO, [UIScreen mainScreen].scale);
[_mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
NSData* imageData = UIImagePNGRepresentation(screenShotImage);
UIGraphicsEndImageContext();
result(nil);
[_channel invokeMethod:@"map#onSnapshot" arguments:imageData];
duzenko marked this conversation as resolved.
Show resolved Hide resolved
} else if ([call.method isEqualToString:@"map#getVisibleRegion"]) {
if (_mapView != nil) {
GMSVisibleRegion visibleRegion = _mapView.projection.visibleRegion;
Expand Down
8 changes: 8 additions & 0 deletions packages/google_maps_flutter/lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class GoogleMapController {
_googleMapState
.onLongPress(LatLng._fromJson(call.arguments['position']));
break;
case 'map#onSnapshot':
_googleMapState.onSnaphot(call.arguments);
break;
default:
throw MissingPluginException();
}
Expand Down Expand Up @@ -204,4 +207,9 @@ class GoogleMapController {

return LatLngBounds(northeast: northeast, southwest: southwest);
}

/// Ask for a snapshot to be returned via onSnapshot event
duzenko marked this conversation as resolved.
Show resolved Hide resolved
Future<void> snapshot() async {
await channel.invokeMethod<void>('map#snapshot');
}
}
11 changes: 11 additions & 0 deletions packages/google_maps_flutter/lib/src/google_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class GoogleMap extends StatefulWidget {
this.onCameraIdle,
this.onTap,
this.onLongPress,
this.onSnapshot,
}) : assert(initialCameraPosition != null),
super(key: key);

Expand Down Expand Up @@ -118,6 +119,9 @@ class GoogleMap extends StatefulWidget {
/// Called every time a [GoogleMap] is long pressed.
final ArgumentCallback<LatLng> onLongPress;

/// Called when a snapshot has completed
duzenko marked this conversation as resolved.
Show resolved Hide resolved
final ArgumentCallback<Uint8List> onSnapshot;

/// True if a "My Location" layer should be shown on the map.
///
/// This layer includes a location indicator at the current device location,
Expand Down Expand Up @@ -334,6 +338,13 @@ class _GoogleMapState extends State<GoogleMap> {
widget.onLongPress(position);
}
}

void onSnaphot(Uint8List imgData) {
assert(imgData != null);
if (widget.onSnapshot != null) {
widget.onSnapshot(imgData);
}
}
}

/// Configuration options for the GoogleMaps user interface.
Expand Down