【问题标题】:Why I am only getting one document using geofirestore为什么我只使用 geofirestore 获得一份文件
【发布时间】:2019-05-31 01:39:54
【问题描述】:

我正在尝试使用geofirestore 查询距用户位置最近的文档,我的代码仅获取 1 个文档,正如您在代码中看到的那样,我将半径设置为 100 公里,并将文档的位置设置为同一地点。

这是我正在查询的文档 这是另一个半径相同的文档

我尝试在地图中打印 snapshot 结果,但它只打印 1 个文档

这里我发送文档 id 以将 query 构建成 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();
            }
        }

我在这里构建查询并将其发送到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();
}

这里是查询结果

【问题讨论】:

  • 你尝试重启路由器了吗?
  • 您确定您在 100 公里半径内有超过 1 个位置吗?
  • 每个文档中的经纬度是我当前的位置

标签: java android google-cloud-firestore geofirestore


【解决方案1】:

我认为您使用的是com.github.imperiumlabs:GeoFirestore-Android,对吧?

从 v1.5.0 版本开始,代码和文档之间存在差异。文档指出queryAtLocation 使用公里

// 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)

但是,代码会从 转换为纬度:

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

Github 上有一个open issue

将仪表传递给 queryAtLocation 应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 2013-11-26
    • 2014-01-17
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2019-05-29
    相关资源
    最近更新 更多