Search code examples
javaandroidgoogle-cloud-firestoregeopoints

How can I retrieve my Geopoint object from firestore


I'm saving my current device location using Firestore's Geopoint object.

On retrieving it, It comes as below (toString()):

{geoPoint=GeoPoint { latitude=-1.2968733, longitude=36.8906349 }}

How can I retrieve the latitude and longitude?


Solution

  • If you know the field is a GeoPoint type object, just fetch it as a GeoPoint type field, then use its getLatitude and getLongitude methods to access those values. Don't bother converting it to a string.

    GeoPoint gp = snapshot.getGeoPoint("your-field");
    double lat = gp.getLatitude();
    double lon = gp.getLongitude();