Andrew Piterov
Andrew Piterov

Reputation: 440

How to make Google map draggable and keep marker in the center of the screen on Flutter?

How to configure google_maps_flutter in Flutter in oder to keep the marker in the center of the map while user will drag the map and get new position with new latitude and longitude, e.g. Uber

enter image description here

Upvotes: 2

Views: 2101

Answers (1)

Tayseer Dalati
Tayseer Dalati

Reputation: 308

Put your GoogleMap inside Stack then put your cursor widget in the Center on the top of your GoogleMap as follow:

  Stack(
    children: [
      GoogleMap(),
      Center(child: CursorrWidget()),
    ],
  );

To calculate your cursor latitude and longitude call this function when notifying onCameraIdle:

GoogleMap(
  onCameraIdle: () {
    LatLngBounds bounds = mapController.getVisibleRegion();
    final lon = (bounds.northeast.longitude + bounds.southwest.longitude) / 2;
    final lat = (bounds.northeast.latitude + bounds.southwest.latitude) / 2;
  }
)

Upvotes: 6

Related Questions