Emin Tırnakçı
Emin Tırnakçı

Reputation: 19

Getting Geopoint Data from Database Using Flutter Firebase Firestore

I am implementing a project in Flutter firebase. In the application, it sends the locations of the places traveled by the user while the user is moving to the firestore database. I also want to get these locations from the database again, but I can't get the geopoints codes. I came across an input called "Instance of 'Geopoints' ". What I want is to get longtitude and latitude as coordinates in the input section that says "Instance of 'Geopoints'". For example (52.2165157N, 6.94378199E)

The piece of code we retrieved from the database:

getData() async {
FirebaseFirestore.instance
    .collection('Test')
    .where(('Liste'), arrayContains: "kosum1")
    .get()
    .then((value) {
  value.docs.forEach((result) {
     print(result.data().values.toString());
  });
});}

The piece of code we recorded in the database:

dataBaseSave() async {
final User user = auth.currentUser;
await _geolocator
    .getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
    .then((Position position) {
  setState(() {
    _currentPosition = position;
    GeoFirePoint point = geo.point(
        latitude: position.latitude, longitude: position.longitude);
    FirebaseFirestore.instance.collection('Test').add({
      'Liste': FieldValue.arrayUnion([nb1, kosuadi, user.uid, point.data])
    });
    nb1++;
  });
});}

The output I get when I run it this way: enter image description here

This is my database: enter image description here

Upvotes: 0

Views: 2632

Answers (4)

Abdul Gani
Abdul Gani

Reputation: 689

Cast like below,

GeoPoint gpoint = result.data().values[3] as GeoPoint;
print(gpoint.latitude);
print(gpoint.longitude);

Upvotes: 0

Emin Tırnakçı
Emin Tırnakçı

Reputation: 19

I changed the query while saving it to the database and this is how I did it. So how should I do the get query from the database while in this state?

dataBaseSave() async {
final User user = auth.currentUser;
await _geolocator
    .getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
    .then((Position position) {
  setState(() {
    _currentPosition = position;
    GeoFirePoint point = geo.point(
        latitude: position.latitude, longitude: position.longitude);
    FirebaseFirestore.instance.collection('Test').add({
      'Liste': FieldValue.arrayUnion(
          [nb1, kosuadi, user.uid, position.latitude, position.longitude])//I changed this place
    });
    nb1++;
  });
});}

And new database datas: enter image description here

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 139039

As I see in your screenshots, each document within your "Test" collection contains an array. At position 0 you have a number, at position 1 and 2 a String, and at position 3 an object. This object contains a geohash and a geopoint. The most important part of your code is:

result.data()

Which returns a Map. So when using the following line of code:

print(result.data().values.toString());

You are only printing the String representation of the Map. To be able to get the value of the geopoint property, you have to iterate through the Map and get the element (object) from the third position, which in terms is also a Map. Iterate again, and get the second element as a GeoPoint object.

Upvotes: 1

You'll have to dig a little more into the data to get the information back. result is going to be an array that holds strings (index 0-2) and an object. That object looks like it has an instance of your GeoPoint. When you access that Geopoint you should be able to do myGeopoint.latitude and myGeopoint.longitude to get the two points. This is not exact, but it should look something like result.data().values[3].geopoint.longitude and same for longitude.

Upvotes: 0

Related Questions