2

I am trying to query the nearest documenst to the user location using geofirestore, my code fetch 1 document only, and as you see in the code I set the radius to 100km, and I set the location of the document from the same location.

This is the document I am querying it document number 1 And this is the other document with the same radius document number 2

I tried to print the snapshot result in a map and it's only print 1 document

here I send the document id to build the query into recyclerView

Map<String,Object> stringIntegerMap=new HashMap<>();
private void getNearestEstate(){
    GeoQuery geoQuery = geoFirestore.queryAtLocation(new GeoPoint(latitude, longitude), 100);
    geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
        @Override
        public void onDocumentEntered(DocumentSnapshot documentSnapshot, GeoPoint geoPoint) {
           stringIntegerMap=documentSnapshot.getData();

            Query query=propertyRef.whereEqualTo("mID",Integer.parseInt(documentSnapshot.getId()));
           dataFetch(query,R.id.discoverRV);

            //here how i tried to print the result to see how many documents i got
            for (Map.Entry<String,Object> entry : stringIntegerMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue().toString();
                Toast.makeText(mContext,key+" "+value,Toast.LENGTH_LONG).show();
            }
        }

And here i build the query and send it to recyclerview adapter

private void dataFetch(Query query,int rvView){
      FirestoreRecyclerOptions<Property> options = new FirestoreRecyclerOptions.Builder<Property>()
        .setQuery(query, Property.class)
        .build();

mAdapter = new DiscoverAdapter(options);
RecyclerView recyclerView = root.findViewById(rvView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
recyclerView.setAdapter(mAdapter);
mAdapter.startListening();
}

here the result of the query enter image description here

ttarchala
  • 4,277
  • 2
  • 26
  • 36
YQadoome
  • 178
  • 14

1 Answers1

0

I think you are using com.github.imperiumlabs:GeoFirestore-Android, right?

As of version v1.5.0, there is a discrepancy between the code and the documentation. The documentation states that queryAtLocation uses kilometers:

// creates a new query around [37.7832, -122.4056] with a radius of 0.6 kilometers
val geoQuery = geoFirestore.queryAtLocation(GeoPoint(37.7832, -122.4056), 0.6)

However, the code converts from meters to latitude:

fun distanceToLatitudeDegrees(distance: Double) = distance / Constants.METERS_PER_DEGREE_LATITUDE

There is an open issue on Github.

Passing meters to the queryAtLocation should solve the problem.

jon
  • 3,202
  • 3
  • 23
  • 35