0

It seems the onMapCreated() method says in document

Callback method for when the map is ready to be used.

I want to take snapshot when the map is rendered. the problem is when I take screenshot the map is not even rendered. I just get the blank screenshot then sometimes it works well!

  ///
  void takeSnapShot() async {
    GoogleMapController controller = await _mapController.future;
    Future<void>.delayed(const Duration(milliseconds: 1500), () async {
      if (mounted) {
        imageBytes = await controller.takeSnapshot();
        setState(() {});
      }
    });
  }

I call this method inside the onMapCreated() method!

Balaji
  • 1,773
  • 1
  • 17
  • 30

1 Answers1

1

You cannot know precisely if the snapshot is going to be blank or not, since there's no API that exposes the GET request future of map tiles, but you could try the following hack of checking for size difference between taken snapshots:

  onMapCreated: (controller) async {
      final Uint8List firstPossibleSnapshot = await controller.takeSnapshot();
      Uint8List currentSnapshot = await controller.takeSnapshot();
      //Arbitrary value, a more precise value can be calculated based on the typical size of default grey tile
      final int sizeDifferenceThreshold =
          firstPossibleSnapshot.elementSizeInBytes ~/ 2;
      //Keep trying till a significant size difference is reached,
      //in production a timeout mechanisim is needed since the tiles may never load due to various reasons (no connection, wrong API key, etc..)
      while (
          (currentSnapshot.lengthInBytes - firstPossibleSnapshot.lengthInBytes)
                  .abs() <
              sizeDifferenceThreshold) {
        currentSnapshot = await controller.takeSnapshot();
      }
      setState(() => _mapSnapshot = currentSnapshot);
    }
Nour Salman
  • 13
  • 1
  • 5