我在firestore中使用了geofirestore,并希望对结果进行分页

时间:2019-02-13 15:36:43

标签: angular google-cloud-firestore geofirestore

技术:geofirestore,firestore,Angular版本7

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

当前:

我正在使用限制,并一次将其增加20。

问题:

这还不够好,因为它每次都调用较大的有效负载,然后随着列表的增加而再次对其进行排序,因此对用户而言确实令人困惑。

请让我知道分页地火的其他选择吗?

当前尝试:

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等?

1 个答案:

答案 0 :(得分:0)

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

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