Home > Enterprise >  How to get GeoPoint values from document
How to get GeoPoint values from document

Time:06-04

I have a list of documents which all hold a coordinate as a value. How can I get all the coordinate and write them into a list. On firebase they are stored as GeoPoints. When I try to get the values from fire base I just get the following.

[Instance of 'GeoPoint', Instance of 'GeoPoint', Instance of 'GeoPoint', ..., Instance of 'GeoPoint', Instance of 'GeoPoint']

Writing to firebase (works)

 _markerClick(lat, long) {
    FirebaseFirestore.instance
        .collection("users")
        .doc(user!.uid)
        .collection('userLocations')
        .add({'coords': GeoPoint(lat, long)});
  }

Reading from firebase

  Future showMarkers() async {
    QuerySnapshot querySnapshot = await FirebaseFirestore.instance
        .collection('users')
        .doc(user!.uid)
        .collection('userLocations')
        .get();

    final allData = querySnapshot.docs.map((doc) => doc.get('coords'));
    
    print(allData);
  }

enter image description here

CodePudding user response:

That seems correct to me: each field is an instance of the GeoPoint class. If you want to get the actual latitude and longitude from the field, use the properties of that object.

querySnapshot.docs.map((doc) => "[${doc.get('coords').latitude}, ${doc.get('coords').longitude}]")

CodePudding user response:

You can try this way too.


  const allCoords = [];
  await FirebaseFirestore.instance
    .collection('users')
    .doc(user!.uid)
    .collection('userLocations')
    .get()
    .then((docs) => {
      docs.forEach((doc) => {
        allCoords.push({
          coords: doc.get('coords'),
        });
      });
    });
  print(allCoords);
  • Related