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

强颜欢笑 提交于 2019-12-02 07:29:24

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!