How to orderBy the closest distance from current location in AngularFire2?

后端 未结 2 1414
星月不相逢
星月不相逢 2021-01-27 02:17

I am using AngularFire2 library to query in firebase. In my firebase, i have data collection and in them, i have many rows with latitude and

相关标签:
2条回答
  • 2021-01-27 03:01

    There are two questions in your post. Let's try to answer each of them.

    Q1: "I need a query which will return 10 rows from the firebase which are at the sortest distance from the current locationList item"

    A1: As you may know, you will not be able to have the "Firebase database querying engine" triggering your calculateDistance() function while querying, i.e. Firebase can only sort based on the methods detailed here https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data.

    In other words, you will have to loop over the entire set of data from the data node in an environment that can execute your function: in your front-end or maybe in a Cloud Function (e.g. called by HTTPS, as a REST API)


    Q2: "Why is that? startAt and endAt should work same as the limitToFirst(10)."

    A2: No they are different.

    As explained in the doc, "startAt() returns items greater than or equal to the specified key or value, depending on the order-by method chosen."

    So first, you have to include a order-by-method at this line

    this.firebaseDBProvider.list('data', ref => ref.startAt(0).endAt(10));
    

    And secondly, depending on the order-by-method, this line is not going to necessarily return 10 records but all the records for which the order-by parameter is between 0 and 10.

    0 讨论(0)
  • 2021-01-27 03:01

    I think, using GeoFire can be a solution to get the data order by closest distance.

    0 讨论(0)
提交回复
热议问题