Search code examples
fluttergoogle-maps-flutter

Flutter adjust/scale google map zoom level based on circle radius


I'd like to scale google map zoom level based on the radius of the circle. Technically I want the user to see circle on map at optimum zoom level. I tried to get bounds and adjust camera position, but circle does not have any such bounds parameter..

GoogleMap(
            onMapCreated: (GoogleMapController controller) {
               controller.showMarkerInfoWindow(MarkerId("0"));
            },
            circles: Set.from([
              Circle(
                circleId: CircleId("0"),
                center: LatLng(...),
                radius: 1000,
              )
            ]),
            initialCameraPosition: CameraPosition(
              target: LatLng(..),
              **zoom: 15, // I want to scale zoom based on radius of circle.**
            ),
            markers: {
              Marker(
                markerId: MarkerId("0"),
                position: LatLng(..),
                infoWindow: InfoWindow(title: ...)
              )
            },
          ),
        ),

Thanks in Advance.


Solution

  • After some search, found a solution here which is in java for android application.

    dart/flutter code would be as follows:

    double getZoomLevel(double radius) {
        double zoomLevel = 11;
        if (radius > 0) {
          double radiusElevated = radius + radius / 2;
          double scale = radiusElevated / 500;
          zoomLevel = 16 - log(scale) / log(2);
        }
        zoomLevel = num.parse(zoomLevel.toStringAsFixed(2));
        return zoomLevel;
      }