How to get marker latitude and longitude on flutter map?

Viewed 2219

I'm using google_maps_flutter package to display the map and I placed a marker so the user can select a location (not the user location any location on the map).
like this Now the user can drag the marker where he want , my question is

Can I get latitude and longitude for marker after user drag it?

here is my full code

class _BodyState extends State<Body> {
  MapType mapType = MapType.normal;
  Completer<GoogleMapController> _controller = Completer();
  LocationServices _locationServices;
  LocationData _locationData;
  CameraPosition _cameraPosition;
  Set<Marker> allMapMarkers;
  Marker userPicker;

  @override
  Widget build(BuildContext context) {
    _locationServices=Provider.of<LocationServices>(context);

    return FutureBuilder(
      future: _locationServices.getLocation().then((LocationData value) => _locationData=value),
      builder:(context,snapshot){
        if(_locationData==null){
         return Center(child: CircularProgressIndicator());
        }else{
          _cameraPosition=CameraPosition(target:LatLng(_locationData.latitude,_locationData.longitude),zoom: 19);
          setupMarkers();   // this set the Lat and Long for marker 
          return SafeArea(
            child: Stack(
              children: [
                GoogleMap(
                  onMapCreated: (GoogleMapController controller) {
                    _controller.complete(controller);
                  },
                  initialCameraPosition: _cameraPosition,
                  mapType: mapType,
                  compassEnabled: false,
                  zoomControlsEnabled: false,
                  markers: allMapMarkers,
                ),
1 Answers

You can use the Marker from google_maps_flutter plugin to fetch marker coordinates. It has an onDrag property that returns a LatLng object that contains coordinates. Use the onDragEnd property to fetch the coordinates on where the marker has been placed.

Marker(
  onTap: () {
    debugPrint('Tapped');
  },
  draggable: true,
  markerId: MarkerId('Marker'),
  onDragEnd: ((LatLng newPosition) {
    debugPrint(newPosition.latitude);
    debugPrint(newPosition.longitude);
  }),
)
Related