Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 303
gavrbhat Avatar asked Nov 06 '20 05:11

gavrbhat


1 Answers

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;
  }
like image 77
gavrbhat Avatar answered Oct 28 '22 16:10

gavrbhat