firestore order by distance code example

Example 1: firestore geopoint query example in android

QueryLocation queryLocation = QueryLocation.fromDegrees(latitude, longitude);
Distance searchDistance = new Distance(1.0, DistanceUnit.KILOMETERS);
geoFire.query()
    .whereEqualTo("title", "The Title")
    .whereNearTo(queryLocation, distance)
    .orderBy("timestamp", Query.Direction.DESCENDING)
    .limit(10)
    .build()
    .get()
    .addOnCompleteListener(task -> {
      if (task.isSuccessful()) {
        Log.i("DB", "Got Documents.");
      } else {
        Log.w("DB", "Error getting documents.", task.getException());
      }
    });

Example 2: how to calculate distance between two addresses in firestore in android studio

QueryLocation queryLocation = QueryLocation.fromDegrees(latitude, longitude);
Distance searchDistance = new Distance(1.0, DistanceUnit.KILOMETERS);
geoFire.query()
    .whereEqualTo("title", "The Title")
    .whereNearTo(queryLocation, distance)
    .orderBy("timestamp", Query.Direction.DESCENDING)
    .limit(10)
    .build()
    .get()
    .addOnCompleteListener(task -> {
      if (task.isSuccessful()) {
        Log.i("DB", "Got Documents.");
      } else {
        Log.w("DB", "Error getting documents.", task.getException());
      }
    });

Tags:

Misc Example