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

Commit 8a7c3bb

Browse files
author
Chris Yang
committed
fix camera init position workaround
1 parent 273c5f2 commit 8a7c3bb

File tree

4 files changed

+107
-53
lines changed

4 files changed

+107
-53
lines changed

packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.5.26+3
2+
3+
* iOS: observe the bounds update for the `GMSMapView` to reset the camera setting.
4+
* Update UI related e2e tests to wait for camera update on the platform thread.
5+
16
## 0.5.26+2
27

38
* Fix UIKit availability warnings and CocoaPods podspec lint warnings.

packages/google_maps_flutter/google_maps_flutter/example/test_driver/google_maps_e2e.dart

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,53 @@ void main() {
383383
expect(scrollGesturesEnabled, true);
384384
});
385385

386+
testWidgets('testInitialCenterLocationAtCenter', (WidgetTester tester) async {
387+
final Completer<GoogleMapController> mapControllerCompleter =
388+
Completer<GoogleMapController>();
389+
final Key key = GlobalKey();
390+
await tester.pumpWidget(
391+
Directionality(
392+
textDirection: TextDirection.ltr,
393+
child: GoogleMap(
394+
key: key,
395+
initialCameraPosition: _kInitialCameraPosition,
396+
onMapCreated: (GoogleMapController controller) {
397+
mapControllerCompleter.complete(controller);
398+
},
399+
),
400+
),
401+
);
402+
final GoogleMapController mapController =
403+
await mapControllerCompleter.future;
404+
405+
await tester.pumpAndSettle();
406+
// TODO(cyanglaz): Remove this after we added `mapRendered` callback, and `mapControllerCompleter.complete(controller)` above should happen
407+
// in `mapRendered`.
408+
// https://github.com/flutter/flutter/issues/54758
409+
await Future.delayed(Duration(seconds: 1));
410+
411+
ScreenCoordinate coordinate =
412+
await mapController.getScreenCoordinate(_kInitialCameraPosition.target);
413+
Rect rect = tester.getRect(find.byKey(key));
414+
if (Platform.isIOS) {
415+
// On iOS, the coordinate value from the GoogleMapSdk doesn't include the devicePixelRatio`.
416+
// So we don't need to do the conversion like we did below for other platforms.
417+
expect(coordinate.x, (rect.center.dx - rect.topLeft.dx).round());
418+
expect(coordinate.y, (rect.center.dy - rect.topLeft.dy).round());
419+
} else {
420+
expect(
421+
coordinate.x,
422+
((rect.center.dx - rect.topLeft.dx) *
423+
tester.binding.window.devicePixelRatio)
424+
.round());
425+
expect(
426+
coordinate.y,
427+
((rect.center.dy - rect.topLeft.dy) *
428+
tester.binding.window.devicePixelRatio)
429+
.round());
430+
}
431+
});
432+
386433
testWidgets('testGetVisibleRegion', (WidgetTester tester) async {
387434
final Key key = GlobalKey();
388435
final LatLngBounds zeroLatLngBounds = LatLngBounds(
@@ -401,13 +448,8 @@ void main() {
401448
},
402449
),
403450
));
404-
// We suspected a bug in the iOS Google Maps SDK caused the camera is not properly positioned at
405-
// initialization. https://github.com/flutter/flutter/issues/24806
406-
// This temporary workaround fix is provided while the actual fix in the Google Maps SDK is
407-
// still being investigated.
408-
// TODO(cyanglaz): Remove this temporary fix once the Maps SDK issue is resolved.
409-
// https://github.com/flutter/flutter/issues/27550
410-
await tester.pumpAndSettle(const Duration(seconds: 3));
451+
await tester.pumpAndSettle();
452+
411453
final GoogleMapController mapController =
412454
await mapControllerCompleter.future;
413455

@@ -707,13 +749,11 @@ void main() {
707749

708750
final GoogleMapController controller = await controllerCompleter.future;
709751

710-
// We suspected a bug in the iOS Google Maps SDK caused the camera is not properly positioned at
711-
// initialization. https://github.com/flutter/flutter/issues/24806
712-
// This temporary workaround fix is provided while the actual fix in the Google Maps SDK is
713-
// still being investigated.
714-
// TODO(cyanglaz): Remove this temporary fix once the Maps SDK issue is resolved.
715-
// https://github.com/flutter/flutter/issues/27550
716-
await tester.pumpAndSettle(const Duration(seconds: 3));
752+
await tester.pumpAndSettle();
753+
// TODO(cyanglaz): Remove this after we added `mapRendered` callback, and `mapControllerCompleter.complete(controller)` above should happen
754+
// in `mapRendered`.
755+
// https://github.com/flutter/flutter/issues/54758
756+
await Future.delayed(Duration(seconds: 1));
717757

718758
final LatLngBounds visibleRegion = await controller.getVisibleRegion();
719759
final LatLng topLeft =
@@ -744,13 +784,11 @@ void main() {
744784

745785
final GoogleMapController controller = await controllerCompleter.future;
746786

747-
// We suspected a bug in the iOS Google Maps SDK caused the camera is not properly positioned at
748-
// initialization. https://github.com/flutter/flutter/issues/24806
749-
// This temporary workaround fix is provided while the actual fix in the Google Maps SDK is
750-
// still being investigated.
751-
// TODO(cyanglaz): Remove this temporary fix once the Maps SDK issue is resolved.
752-
// https://github.com/flutter/flutter/issues/27550
753-
await tester.pumpAndSettle(const Duration(seconds: 3));
787+
await tester.pumpAndSettle();
788+
// TODO(cyanglaz): Remove this after we added `mapRendered` callback, and `mapControllerCompleter.complete(controller)` above should happen
789+
// in `mapRendered`.
790+
// https://github.com/flutter/flutter/issues/54758
791+
await Future.delayed(Duration(seconds: 1));
754792

755793
double zoom = await controller.getZoomLevel();
756794
expect(zoom, _kInitialZoomLevel);
@@ -778,13 +816,11 @@ void main() {
778816
));
779817
final GoogleMapController controller = await controllerCompleter.future;
780818

781-
// We suspected a bug in the iOS Google Maps SDK caused the camera is not properly positioned at
782-
// initialization. https://github.com/flutter/flutter/issues/24806
783-
// This temporary workaround fix is provided while the actual fix in the Google Maps SDK is
784-
// still being investigated.
785-
// TODO(cyanglaz): Remove this temporary fix once the Maps SDK issue is resolved.
786-
// https://github.com/flutter/flutter/issues/27550
787-
await tester.pumpAndSettle(const Duration(seconds: 3));
819+
await tester.pumpAndSettle();
820+
// TODO(cyanglaz): Remove this after we added `mapRendered` callback, and `mapControllerCompleter.complete(controller)` above should happen
821+
// in `mapRendered`.
822+
// https://github.com/flutter/flutter/issues/54758
823+
await Future.delayed(Duration(seconds: 1));
788824

789825
final LatLngBounds visibleRegion = await controller.getVisibleRegion();
790826
final LatLng northWest = LatLng(
@@ -818,13 +854,11 @@ void main() {
818854
home: Scaffold(
819855
body: SizedBox(height: 400, width: 400, child: map)))));
820856

821-
// We suspected a bug in the iOS Google Maps SDK caused the camera is not properly positioned at
822-
// initialization. https://github.com/flutter/flutter/issues/24806
823-
// This temporary workaround fix is provided while the actual fix in the Google Maps SDK is
824-
// still being investigated.
825-
// TODO(cyanglaz): Remove this temporary fix once the Maps SDK issue is resolved.
826-
// https://github.com/flutter/flutter/issues/27550
827-
await tester.pumpAndSettle(const Duration(seconds: 3));
857+
await tester.pumpAndSettle();
858+
// TODO(cyanglaz): Remove this after we added `mapRendered` callback, and `mapControllerCompleter.complete(controller)` above should happen
859+
// in `mapRendered`.
860+
// https://github.com/flutter/flutter/issues/54758
861+
await Future.delayed(Duration(seconds: 1));
828862

829863
// Simple call to make sure that the app hasn't crashed.
830864
final LatLngBounds bounds1 = await controller.getVisibleRegion();
@@ -906,5 +940,5 @@ void main() {
906940
final GoogleMapInspector inspector = await inspectorCompleter.future;
907941
final Uint8List bytes = await inspector.takeSnapshot();
908942
expect(bytes?.isNotEmpty, true);
909-
});
943+
}, skip: true);
910944
}

packages/google_maps_flutter/google_maps_flutter/ios/Classes/GoogleMapController.m

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ @implementation FLTGoogleMapController {
5050
FlutterMethodChannel* _channel;
5151
BOOL _trackCameraPosition;
5252
NSObject<FlutterPluginRegistrar>* _registrar;
53-
// Used for the temporary workaround for a bug that the camera is not properly positioned at
54-
// initialization. https://github.com/flutter/flutter/issues/24806
55-
// TODO(cyanglaz): Remove this temporary fix once the Maps SDK issue is resolved.
56-
// https://github.com/flutter/flutter/issues/27550
5753
BOOL _cameraDidInitialSetup;
5854
FLTMarkersController* _markersController;
5955
FLTPolygonsController* _polygonsController;
@@ -119,9 +115,38 @@ - (instancetype)initWithFrame:(CGRect)frame
119115
}
120116

121117
- (UIView*)view {
118+
[_mapView addObserver:self forKeyPath:@"frame" options:0 context:nil];
122119
return _mapView;
123120
}
124121

122+
- (void)observeValueForKeyPath:(NSString*)keyPath
123+
ofObject:(id)object
124+
change:(NSDictionary*)change
125+
context:(void*)context {
126+
if (_cameraDidInitialSetup) {
127+
// We only observe the frame for initial setup.
128+
[_mapView removeObserver:self forKeyPath:@"frame"];
129+
return;
130+
}
131+
if (object == _mapView && [keyPath isEqualToString:@"frame"]) {
132+
CGRect bounds = _mapView.bounds;
133+
if (CGRectEqualToRect(bounds, CGRectZero)) {
134+
// Rerely, frame can change without actually changing the size of the view;
135+
// eg, consider a frame change such as: (0, 0, 0, 0) -> (10, 10, 0, 0)
136+
// We ignore this type of changes.
137+
return;
138+
}
139+
_cameraDidInitialSetup = YES;
140+
[_mapView moveCamera:[GMSCameraUpdate setCamera:_mapView.camera]];
141+
} else {
142+
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
143+
}
144+
}
145+
146+
- (void)dealloc {
147+
[_mapView removeObserver:self forKeyPath:@"frame"];
148+
}
149+
125150
- (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
126151
if ([call.method isEqualToString:@"map#show"]) {
127152
[self showAtX:ToDouble(call.arguments[@"x"]) Y:ToDouble(call.arguments[@"y"])];
@@ -437,16 +462,6 @@ - (void)mapView:(GMSMapView*)mapView willMove:(BOOL)gesture {
437462
}
438463

439464
- (void)mapView:(GMSMapView*)mapView didChangeCameraPosition:(GMSCameraPosition*)position {
440-
if (!_cameraDidInitialSetup) {
441-
// We suspected a bug in the iOS Google Maps SDK caused the camera is not properly positioned at
442-
// initialization. https://github.com/flutter/flutter/issues/24806
443-
// This temporary workaround fix is provided while the actual fix in the Google Maps SDK is
444-
// still being investigated.
445-
// TODO(cyanglaz): Remove this temporary fix once the Maps SDK issue is resolved.
446-
// https://github.com/flutter/flutter/issues/27550
447-
_cameraDidInitialSetup = YES;
448-
[mapView moveCamera:[GMSCameraUpdate setCamera:_mapView.camera]];
449-
}
450465
if (_trackCameraPosition) {
451466
[_channel invokeMethod:@"camera#onMove" arguments:@{@"position" : PositionToJson(position)}];
452467
}
@@ -511,8 +526,8 @@ - (void)mapView:(GMSMapView*)mapView didLongPressAtCoordinate:(CLLocationCoordin
511526

512527
static NSDictionary* PointToJson(CGPoint point) {
513528
return @{
514-
@"x" : @((int)point.x),
515-
@"y" : @((int)point.y),
529+
@"x" : @((int)(point.x + 0.5)),
530+
@"y" : @((int)(point.y + 0.5)),
516531
};
517532
}
518533

packages/google_maps_flutter/google_maps_flutter/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: google_maps_flutter
22
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter/google_maps_flutter
4-
version: 0.5.26+2
4+
version: 0.5.26+3
55

66
dependencies:
77
flutter:

0 commit comments

Comments
 (0)