【问题标题】:Im using geofirestore with firestore and want to paginate the results我将 geofirestore 与 firestore 一起使用,并希望对结果进行分页
【发布时间】:2019-02-13 15:36:43
【问题描述】:

技术:geofirestore、firestore、angular 版本 7

链接到 geofirestore:https://github.com/geofirestore/geofirestore-js

目前:

我正在使用限制并一次增加 20 个。

问题:

它还不够好,因为它每次调用更大的有效载荷,然后随着列表的升序增长再次对其进行排序,因此对用户来说真的很困惑。

请告诉我分页 geofire 的任何替代方法?

当前尝试:

public getResults(geo: any, lastLimit: number) {
  const geocollection: GeoCollectionReference = this.geofirestore.collection('test');
  const query = geocollection.near({ center: new firebase.firestore.GeoPoint(geo.lat, geo.lng), radius: geo.radius });
  lastLimit = lastLimit + 2;

  return query.limit(lastLimit).get().then((querySnapshot) => {
    return this.mapResponse(querySnapshot, lastLimit);
  });
}


public mapResponse(querySnapshot: GeoQuerySnapshot, lastLimit: number): any {
   let jobs = [];

   querySnapshot.forEach((doc) => {
    jobs = [ ...jobs, doc.data() ];
 });

 return { jobs, lastLimit };
}

问题:

  1. 如果没有分页,什么时候可以使用?

  2. 如果它不可用,我现在如何解决这个问题?

  3. 如果可用,我该怎么做,通常我使用 startAt / startAfter 等?

【问题讨论】:

  • 您希望结果的顺序是什么?因为如果您希望首先获得 20 个最接近的结果,然后按距离排序接下来的 20 个,那么如果不显着改变 GeoFire/GeoFirestore 的工作方式,这将是不可能的。见stackoverflow.com/a/54603092
  • @FrankvanPuffelen 我想通过最接近搜索的 lat 和 lng 来订购它们,例如半径10可以说。但它不允许我为 geofire 执行 orderBy 或 startAt
  • 没错,没有办法只得到最接近的结果。请参阅我的链接答案,了解为什么会这样。随意向您使用的 GeoFirestore 库提交功能请求。但是考虑到地理查询在这些库中的工作方式,如果不简单地读取范围内的所有内容,那么有人极不可能找到一种方法在它们之上实现“最近”。
  • 当然。加载范围内的所有文档,按距离排序,取前 20 个。这正是我的链接答案所描述的。

标签: angular google-cloud-firestore geofirestore


【解决方案1】:

从中心位置检索项目列表,给定半径限制为一定数量的项目。

val db = FirebaseFirestore.getInstance()

/*Create two object to pass location and distance for radius*/
val centerLocation = Location(centerLatitude, centerLongitude)
val distanceForRadius = Distance(1.0, DistanceUnit.KILOMETERS) // or you can set unit as DistanceUnit.MILES if you want to find for 1 mile

val geoQuery = GeoQuery()
        .collection("users")
        .whereEqualTo("status","approved")
        .whereEqualTo("country","IN")
        .whereNearToLocation(centerLocation, distanceForRadius, fieldName) 
        //fieldName if you have passed at time of setLocation else it will take default as "g" if you do not pass
        .startAfter(lastDocument) //optinal (for pagination)
        .limit(10) // If you requires only 10 data per query

参考 https://github.com/chintan369/Geo-FireStore-Query

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多