Thanks to visit codestin.com
Credit goes to github.com

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

[google_maps_flutter] Fix the visual jarring during the first gesture on the map #2629

Merged
merged 5 commits into from
Apr 23, 2020

Conversation

cyanglaz
Copy link
Contributor

@cyanglaz cyanglaz commented Apr 1, 2020

Description

Instead of workaround in the first delegate callback. We reset the camera location when we know the frame of the GMSMapView is changed. This prevents any unwanted artifacts introduced due to the previous workaround.

Related Issues

flutter/flutter#53428
flutter/flutter#27550

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

@cyanglaz
Copy link
Contributor Author

cyanglaz commented Apr 1, 2020

We don't have a good way currently to test the visual animation on google map, can we get an exemption from testing? As it is a simple change. @amirh

@amirh
Copy link
Contributor

amirh commented Apr 1, 2020

test?

change:(NSDictionary*)change
context:(void*)context {
if (object == _mapView && [keyPath isEqualToString:@"frame"]) {
[_mapView moveCamera:[GMSCameraUpdate setCamera:_mapView.camera]];
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the intended behavior if a the map frame is resized while it's being animated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In that case, this method will get called multiple times. However, since we are always setting the camera to the same location, it shouldn't have any behavior effects.

Copy link
Contributor

Choose a reason for hiding this comment

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

animateWithCameraUpdate is implemented by multiple calls to moveCamera?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made this only happens once during initialization, so it shouldn't have any side effects that you worried about.

// `onMapCreated` is called before the map is actually ready on the platform thread.
// Wait for a second until the map is settled down.
await tester.pumpAndSettle();
await Future.delayed(Duration(seconds: 1));
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a reliable way to detect when it's ready?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unless we add a new callback API in the google map, something like "onMapRendered". This can be in a separate PR tho.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should do that, I'm not blocking this PR since we're trading a 3 seconds timer with a 1 second one , but we should at least add a TODO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do!

@cyanglaz
Copy link
Contributor Author

@amirh Ready for another review.

@ditman
Copy link
Member

ditman commented Apr 21, 2020

This PR is compatible with the upcoming google_maps_flutter federation PR.

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@cyanglaz cyanglaz force-pushed the google_map_example_app_fix branch from 4bbb047 to ba74639 Compare April 21, 2020 20:00
@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@cyanglaz cyanglaz force-pushed the google_map_example_app_fix branch from ba74639 to 5fea81d Compare April 21, 2020 20:07
@cyanglaz cyanglaz force-pushed the google_map_example_app_fix branch from 5fea81d to 8a7c3bb Compare April 21, 2020 20:30
// We ignore this type of changes.
return;
}
_cameraDidInitialSetup = YES;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not remove the observer here instead of on the next frame change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point! Thanks. will change.

@@ -511,8 +526,8 @@ - (void)mapView:(GMSMapView*)mapView didLongPressAtCoordinate:(CLLocationCoordin

static NSDictionary* PointToJson(CGPoint point) {
return @{
@"x" : @((int)point.x),
@"y" : @((int)point.y),
@"x" : @((int)(point.x + 0.5)),
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 0.5?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to round to the closest int.
The current code is getting a floor, which is less accurate.
updating to use lroundf to make it more self explanatory.

CGRect bounds = _mapView.bounds;
if (CGRectEqualToRect(bounds, CGRectZero)) {
// Rerely, frame can change without actually changing the size of the view;
// eg, consider a frame change such as: (0, 0, 0, 0) -> (10, 10, 0, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

You mean (0, 0, 0, 0) -> (10, 10, 10, 10) ?
Is this not the change we get any time we "move" a map? (is that rare?)

I see why we can ignore these changes - this whole thing is working around an issue with our initial frame being sized to zero, but if a consequent frame is zero there's nothing to workaround really... (what I'm saying the code makes sense to me, the comment makes less sense)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about just:

      // The workaround is to fix an issue that the camera location is not current when
      // the size of the map is zero at initialization.
      // So We only care about the size of the `_mapView`, ignore the frame changes when the size is zero.

@@ -906,5 +940,5 @@ void main() {
final GoogleMapInspector inspector = await inspectorCompleter.future;
final Uint8List bytes = await inspector.takeSnapshot();
expect(bytes?.isNotEmpty, true);
});
}, skip: true);
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we skipping this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

skipped by mistake, will fix

Copy link
Contributor Author

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

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

@amirh Updated based on your comment! PTAL

@@ -906,5 +940,5 @@ void main() {
final GoogleMapInspector inspector = await inspectorCompleter.future;
final Uint8List bytes = await inspector.takeSnapshot();
expect(bytes?.isNotEmpty, true);
});
}, skip: true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

skipped by mistake, will fix

CGRect bounds = _mapView.bounds;
if (CGRectEqualToRect(bounds, CGRectZero)) {
// Rerely, frame can change without actually changing the size of the view;
// eg, consider a frame change such as: (0, 0, 0, 0) -> (10, 10, 0, 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about just:

      // The workaround is to fix an issue that the camera location is not current when
      // the size of the map is zero at initialization.
      // So We only care about the size of the `_mapView`, ignore the frame changes when the size is zero.

@@ -511,8 +526,8 @@ - (void)mapView:(GMSMapView*)mapView didLongPressAtCoordinate:(CLLocationCoordin

static NSDictionary* PointToJson(CGPoint point) {
return @{
@"x" : @((int)point.x),
@"y" : @((int)point.y),
@"x" : @((int)(point.x + 0.5)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to round to the closest int.
The current code is getting a floor, which is less accurate.
updating to use lroundf to make it more self explanatory.

// We ignore this type of changes.
return;
}
_cameraDidInitialSetup = YES;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point! Thanks. will change.

Copy link
Contributor

@amirh amirh left a comment

Choose a reason for hiding this comment

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

LGTM

(once presubmits pass)

@cyanglaz
Copy link
Contributor Author

cyanglaz commented Apr 23, 2020

the crash seems to be something new related to google map sdk
https://stackoverflow.com/questions/61397027/androidruntime-fatal-exception-androidmapsapi-zoomtablemanager-java-lang-arra
b/154855417

@cyanglaz cyanglaz merged commit 531ff2b into flutter:master Apr 23, 2020
@cyanglaz cyanglaz deleted the google_map_example_app_fix branch April 23, 2020 23:22
EdwinRomelta pushed a commit to EdwinRomelta/plugins that referenced this pull request Jun 11, 2020
jorgefspereira pushed a commit to jorgefspereira/plugins_flutter that referenced this pull request Oct 10, 2020
FlutterSu pushed a commit to FlutterSu/flutter-plugins that referenced this pull request Nov 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants