Home > Software design >  How to Set Zoom Level For google_maps_flutter
How to Set Zoom Level For google_maps_flutter

Time:10-08

I am using the google_maps_flutter package and the Zoom level for the myLocationButton, when pressed, is set to my last zoom level. I would like to reset the Zoom level when the myLocationButton is pressed.

This is the code for the Google Maps;

GoogleMap(
                      padding: EdgeInsets.only(bottom: _mapBottomPadding),
                      mapType: MapType.normal,
                      initialCameraPosition: _baseLocation,
                      myLocationButtonEnabled: true,
                      myLocationEnabled: true,
                      
                      zoomControlsEnabled: true,
                      zoomGesturesEnabled: true,
                      // minMaxZoomPreference: const MinMaxZoomPreference(12, 14),
                      polylines: _polylineSet,
                      markers: _markersSet,
                      onMapCreated: (GoogleMapController controller) async {
                        if (!_controller.isCompleted) {
                          //first calling is false
                          //call "completer()"
                          _controller.complete(controller);
                          // setState(() {});
                        } else {
                          //other calling, later is true,
                          //don't call again completer()
                        }
                        // _controller.complete(controller);
                        _googleMapController = controller;

                        // Set Initial Camera Position
                        setCameraPosition(
                            position: locationData.getCurrentAddress.position
                                as Position);
                        setMapBounds();
                        setState(() {});
                      },
                    ),

The zoom is stuck on the last zoom level when setting camera position previously and I would like to rest it when I click the myLocationButton.

The setMapBounds(); method call sets the zoom which depends on map bounds and calls the code below which can result in a high zoom level and the zoom level persists after the call, when I click the myLocationButton.

_googleMapController!
          .animateCamera(CameraUpdate.newLatLngBounds(latLngBounds, 70));

How can I reset the zoom level after animating the camera?

CodePudding user response:

Can you please try this one if it helps.try to setState zoomValue

double zoomValue=14.0;

         LatLng   mapCenter = new LatLng("your latitude", "your longitude");
    
    initialCameraPosition: CameraPosition(
                      target: mapCenter,
                      zoom: zoomValue,
                    ),

CodePudding user response:

Try this

GoogleMap(
  zoomGesturesEnabled: true,
  tiltGesturesEnabled: false,
  onCameraMove:(CameraPosition cameraPosition){
     print(cameraPosition.zoom);
  },
  • Related