2

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?

3 Answers3

2

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,
                    ),
Mofidul Islam
  • 378
  • 3
  • 12
0

Try this

GoogleMap(
  zoomGesturesEnabled: true,
  tiltGesturesEnabled: false,
  onCameraMove:(CameraPosition cameraPosition){
     print(cameraPosition.zoom);
  },
G H Prakash
  • 1,720
  • 10
  • 30
0
set zoomValue = 3;
LatLng   mapCenter = new LatLng("your latitude", "your longitude");

initialCameraPosition: CameraPosition(
                  target: mapCenter,
                  zoom: zoomValue,
                ),
jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Apr 18 '23 at 01:09