How to convert GeoPoint in Firestore to LatLng
Asked Answered
C

3

7

Hi I'm using Android Studio and I'm having trouble of converting data that I've got from Firestore. I saved data in my Firestore type GeoPoint and I want to query it and convert to Object type LatLng.

Here is my code:

    final FirebaseFirestore db = FirebaseFirestore.getInstance();
    final CollectionReference stores = db.collection("stores");
    stores.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.i(TAG,document.getData().get("position").toString());
                        }
                    } else {
                        Log.w(TAG, "Error getting documents.", task.getException());
                    }
                }
            });

and Here is what i got

    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=29.339555, longitude=169.715858 }
    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=68.085393, longitude=-42.081575 }
    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=16.503923, longitude=90.196118 }
    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=-69.424524, longitude=-71.356333 }
    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=69.502257, longitude=71.100474 }

All i need is to get latitude,longtitude value to set into LatLng object.

Celestial answered 16/12, 2018 at 4:6 Comment(0)
C
3

You need to extract the Latitude and Longitude from the Geopoint object received,

and make a new LatLng object, like so

double lat = document.getData().get("position").getLatitude();
double lng = document.getData().get("position").getLongitude ();
LatLng latLng = new LatLng(lat, lng);
Cortese answered 16/12, 2018 at 4:19 Comment(2)
How to extract the Latitude and Longitude from the Geopoint I'm sorry I'm a beginner of android studio Thank you very muchCelestial
@SupagonSrisawas check the docs developers.google.com/android/reference/com/google/firebase/…Cortese
L
14

To solve this, you can simply use getGeoPoint() method like this:

GeoPoint geoPoint = document.getGeoPoint("position");

And then use:

double lat = geoPoint.getLatitude();
double lng = geoPoint.getLongitude();
LatLng latLng = new LatLng(lat, lng);
Levelheaded answered 16/12, 2018 at 9:43 Comment(5)
how if in nested object?. Products products = documentSnapshot.toObject(Products.class); System.out.println(products.getLocation().get("geoPoint").toString());Grijalva
@LatiefAnwar In that case you should simply use: System.out.println(products.getLocation().toString()); which will return a Geopoint objct. If you have hard time solving that, please post another fresh question using its own MCVE, so me and other Firebase developers can help you.Levelheaded
can't we simplify it more by LatLng latLng = new LatLng(document.getGeoPoint("position").getLatitude(), document.getGeoPoint("position").getLongitude());Biblio
@IzhanAli You can do it if you want.Levelheaded
Hey Supagon. Can you please explain what was wrong with my answer?Levelheaded
S
5

This has worked for me with flutter cloud_firestore

void getData() {
databaseReference
    .collection("stores")
    .getDocuments()
    .then((QuerySnapshot snapshot) {
  snapshot.documents.forEach((f) {
    print('${f.data}}');
    GeoPoint pos = f.data['position'];
    LatLng latLng = new LatLng(pos.latitude, pos.longitude);

  });
});
}

Reference - https://fireship.io/lessons/flutter-realtime-geolocation-firebase/

Sobriety answered 4/4, 2020 at 20:32 Comment(0)
C
3

You need to extract the Latitude and Longitude from the Geopoint object received,

and make a new LatLng object, like so

double lat = document.getData().get("position").getLatitude();
double lng = document.getData().get("position").getLongitude ();
LatLng latLng = new LatLng(lat, lng);
Cortese answered 16/12, 2018 at 4:19 Comment(2)
How to extract the Latitude and Longitude from the Geopoint I'm sorry I'm a beginner of android studio Thank you very muchCelestial
@SupagonSrisawas check the docs developers.google.com/android/reference/com/google/firebase/…Cortese

© 2022 - 2024 — McMap. All rights reserved.