Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert GeoPoint in Firestore to LatLng

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.

like image 427
Supagon Srisawas Avatar asked Dec 16 '18 04:12

Supagon Srisawas


1 Answers

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);
like image 189
Alex Mamo Avatar answered Sep 28 '22 09:09

Alex Mamo