Search code examples
firebasefluttergoogle-mapsgoogle-cloud-firestoremarker

How I can get lat and long of marker then store in firestore - flutter


I want to get the latitude and longitude of the marker in this code for store this location in firebase

body: GoogleMap(
       zoomControlsEnabled: false,
       initialCameraPosition: CameraPosition(target: latLng ,zoom: 14.34),
       markers: markers,
    //  onTap: _handleTap,
       onMapCreated: (GoogleMapController controller) {
           _controller.complete(controller);
         },
       onCameraMove: (point){
         setState(() {
           markers.add(Marker(markerId: markerId,position: point.target)
           );
         });
         
       },
     ),

After get lat and long of new position of marker I want store new position in firestore

This my code

Completer<GoogleMapController> _controller = Completer();
 final markers = Set<Marker>();
 MarkerId markerId = MarkerId('parking');
 LatLng latLng = LatLng(32.5259483,35.8506622);

TextButton(onPressed: (){
                   FirebaseFirestore.instance.collection('parking_go_loc').add({
                         'name':'${name}',
                         'email':'${email}',
       'location' : GeoPoint(latLng.latitude,latLng.longitude),
       
                        });
                        Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=> LoginWoner()));
                 }

my packages

google_maps_flutter: 
 cloud_firestore: ^2.4.0
 location: ^4.2.0
 firebase_core :

But When I run this code it stores the first coordinates I put in code It does not store the new coordinates of the marker after moving them and pressing the save button


Solution

  • You just need to update the latLng variable to the new LatLng value in the onCameraMove callback.

    Add this line latLng = point.target and your onCameraMove will be:

    onCameraMove: (point){
      setState(() {
        markers.add(Marker(markerId: markerId,position: point.target)
        latLng = point.target;
      );
    );