Issue with getting current position of camera in google_maps_flutter

Issue

I’m a novice in Flutter, and I encountered an issue with google maps plugin. I watched a couple of tutorials in order to get the current position of the camera and Most of them was using GoogleMapController.cameraPosition.target. I think they deleted this method from the controller(Since it is still on the development stage). Is there any other way of getting the current position of the camera?

Solution

If you are using the google_maps_flutter package:

The GoogleMap widget have the onCameraMove function that returns the CameraPosition while dragging the map or moving the camera.

To do this, you’ll need to create a callback function called _getCameraPosition(CameraPosition cameraPosition which will be invoked when onCameraMove is called. For example:

void _getCameraPosition(CameraPosition cameraPosition) {
   // You can do whatever you want with cameraPosition here
   log("cameraPosition: " + cameraPosition.target.toString());
}

Then, you’ll need to put the _getCameraPosition function to the onCameraMove field on GoogleMap widget, like this:

GoogleMap(
   onCameraMove: _getCameraPosition,    // pass it here
   onMapCreated: _onMapCreated,
   initialCameraPosition: CameraPosition(
       target: LatLng(-33.86882, 151.209296),
       zoom: 12,
   ),
 ),

As a result, you will get a LatLng value in the debug console. For example:

cameraPosition: LatLng(-33.8940124943736, 151.2027569487691)

Answered By – jabamataro

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.