Repository: flutter
Issue: [google_maps_flutter] [iOS] animateCamera() freezes or fails when map is loaded but not visible
Labels: platform-ios,c: performance,p: maps,package,perf: speed,has reproducible steps,P2,found in release: 3.0,found in release: 3.1,team-ios,triaged-ios



### Description
On iOS `googleMapsController.animateCamera()` will freeze or fail when it is called when the map is not (or only just) visible.
For example, having the map loaded in an IndexedStack and you trigger a camera animation from a second page while returning to the map.

This issue does not appear on Android, where the camera animations work like expected.

Versions:
- google_maps_flutter: 2.1.7
- Flutter: 2.10.5
- iOS 15.x

I tested with flutter 2.10.5 because flutter 3 breaks some of my apps and I would have to install deinstall the entire SDK.

### Steps to Reproduce
1. Run the code sample in a project with `google_maps_flutter` configured with API keys etc.
2. Go to the second tab of the BottomNavigationBar.
3. Click the buttons to move to a location on the map. (1 is new york, 2 is Washington).
4. `1 static` and `2 static` are not animated and will simply show the correct locations.
5. Buttons `1 animated` and `2 animated` show the issue in the animation.
6. Buttons `n animated + delay` shows that the bug does not happen if you wait for the map to get ready first.

### Expected results:
The IndexedStack switches to the map and the map animates to the designated location, just like it does on Android.

### Actual results: 
On iOS the camera animation freezes or lags.
- In some cases the map will stop at a random location along the animation and will not update again until a user gesture or `setState` is called.
- In other cases it will freeze at a location along the animation, and after a few seconds jump to the destination.

This occurs both on real devices and simulators.

Adding a delay between the indexedstack showing the map and starting the animation avoids the issue. Which makes it look like the map or PlatformView is not ready yet.

## Videos:
Button order in these videos in case it isn't clear: 
`1 static`, `2 static`, `1 animated`, `2 animated`, `1 animated + delay`, `2 animated + delay` and back to animated.

<details>
<summary> Android has no issue. </summary>

https://user-images.githubusercontent.com/11444698/172818084-6aaf1c32-aa46-4e87-ba14-487d63c52472.mp4
</details>

<details>
<summary>  Iphone 13 pro simulator shows the map freezing until user iteraction. </summary>

https://user-images.githubusercontent.com/11444698/172818093-fd8828a2-be83-4b24-964f-5a01059892e2.mp4
</details>

<details>
<summary>  Ipad pro shows the map freezing for a few seconds before showing the destination. </summary>

https://user-images.githubusercontent.com/11444698/172818102-dd2d0a02-ace6-41cc-8311-d3774cf5ad82.mp4
</details>








## Code sample:

<details>
<summary>Google Maps in an IndexedStack and a secondary page with buttons to interact with the map.</summary>

```
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapPage extends StatefulWidget {
  const MapPage({Key? key}) : super(key: key);

  @override
  _MapPageState createState() => _MapPageState();
}

//use WidgetsBindingObserver to handle app resume
class _MapPageState extends State<MapPage> with WidgetsBindingObserver {
  final Completer<GoogleMapController> _controller = Completer();
  int _selectedIndex = 0;

  static const CameraPosition _initialPosition = CameraPosition(
    target: LatLng(40.00916037755595, -79.03188044266467),
    zoom: 4,
  );

  // New York
  static const CameraPosition _position1 = CameraPosition(
    target: LatLng(40.72602973969118, -73.99273265635692),
    zoom: 12,
  );

  // Washington
  static const CameraPosition _position2 = CameraPosition(
    target: LatLng(38.87511198787617, -77.02867230595746),
    zoom: 12,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _selectedIndex,
        children: [
          GoogleMap(
            mapType: MapType.normal,
            initialCameraPosition: _initialPosition,
            compassEnabled: false,
            rotateGesturesEnabled: false,
            zoomControlsEnabled: false,
            myLocationEnabled: true,
            myLocationButtonEnabled: false,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
            },
          ),
          //simple page with some buttons
          Center(
            child: Column(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      MaterialButton(
                        color: Colors.blue,
                        child: const Text("1 animated"),
                        onPressed: () => _move(_position1, animated: true),
                      ),
                      MaterialButton(
                        color: Colors.blue,
                        child: const Text("1 static"),
                        onPressed: () => _move(_position1, animated: false),
                      ),
                      MaterialButton(
                        color: Colors.blue,
                        child: const Text("1 animated + delay"),
                        onPressed: () =>
                            _move(_position1, animated: true, delayed: true),
                      ),
                    ]),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    MaterialButton(
                      color: Colors.blue,
                      child: const Text("2 animated"),
                      onPressed: () => _move(_position2, animated: true),
                    ),
                    MaterialButton(
                      color: Colors.blue,
                      child: const Text("2 static"),
                      onPressed: () => _move(_position2, animated: false),
                    ),
                    MaterialButton(
                      color: Colors.blue,
                      child: const Text("2 animated + delay"),
                      onPressed: () =>
                          _move(_position2, animated: true, delayed: true),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _selectedIndex,
        onTap: (int index) {
          setState(() {
            _selectedIndex = index;
          });
        },
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.map), label: "map"),
          BottomNavigationBarItem(icon: Icon(Icons.list), label: "other"),
        ],
      ),
    );
  }

  void _move(CameraPosition location,
      {bool animated = true, bool delayed = false}) async {
    setState(() {
      _selectedIndex = 0;
    });
    if (delayed) {
      await Future.delayed(const Duration(milliseconds: 100));
    }
    final GoogleMapController controller = await _controller.future;
    if (animated) {
      controller.animateCamera(CameraUpdate.newCameraPosition(location));
    } else {
      controller.moveCamera(CameraUpdate.newCameraPosition(location));
    }
  }
}
```
</details>

## Flutter Doctor --verbose:
<details>
  <summary>Flutter doctor Mac (iOS builds only, no Android configured)</summary>

```
[✓] Flutter (Channel stable, 2.10.5, on macOS 12.3.1 21E258 darwin-arm, locale nl-NL)
    • Flutter version 2.10.5 at /Users/XXXXX/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5464c5bac7 (7 weeks ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/XXXXX/Library/Android/sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: /usr/bin/java
    ✗ Could not determine java version

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] VS Code (version 1.67.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.42.0

[✓] Connected device (3 available)
    • iPad XXXXX          (mobile) • XXXXXXb79c24b51a5a4eb9599b50f298487XXXXX • ios            • iOS 15.4.1 19E258
    • iPhone 13 Pro (mobile)       • XXXXXXXX-355D-4715-B645-FB2249F70770     • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-5 (simulator)
    • Chrome (web)                 • chrome                                   • web-javascript • Google Chrome 102.0.5005.61

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.

```

</details>

<details>
  <summary>Flutter doctor Windows (Android build)</summary>

```
[√] Flutter (Channel stable, 2.10.5, on Microsoft Windows [Version 10.0.19044.1706], locale en-GB)
    • Flutter version 2.10.5 at C:\Flutter\flutter_windows_2.10.4-stable\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5464c5bac7 (7 weeks ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[√] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at C:\Users\HNK\AppData\Local\Android\sdk
    • Platform android-32, build-tools 32.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.11)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.32228.343
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2021.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)

[√] VS Code (version 1.67.2)
    • VS Code at C:\Users\HNK\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.42.0

[√] Connected device (4 available)
    • SM G780G (mobile) • RF8R8021WSR • android-arm64  • Android 12 (API 31)
    • Windows (desktop) • windows     • windows-x64    • Microsoft Windows [Version 10.0.19044.1706]
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 101.0.4951.67
    • Edge (web)        • edge        • web-javascript • Microsoft Edge 102.0.1245.33

[√] HTTP Host Availability
    • All required HTTP hosts are available
```

</details>