如何在 AngularFire2 中按距离当前位置最近的距离排序? [英] How to orderBy the closest distance from current location in AngularFire2?

查看:23
本文介绍了如何在 AngularFire2 中按距离当前位置最近的距离排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AngularFire2 库在 firebase 中进行查询.在我的 firebase 中,我有 data 集合,其中有很多行带有 latitudelongitude,如下所示:

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 longitude like below:

- data
    - 1404033346513076_1998422263740845
        - location
           - latitude: 23.7907795
           - longitude: 90.403898
    - 1404033346513076_1998422263740888
        - location
           - latitude: 23.9907795
           - longitude: 91.403898

我有我当前位置的 latitudelongitude 并且我有一个辅助函数,可以确定当前位置和距离火力基地的位置之间的距离(以米为单位)数据.假设函数如下:

I have the latitude and longitude of my current location and i have a helper function which can determine the distance in meters between the current location and a location from the firebase data. Suppose, the function is like below:

calculateDistance(currentLoc, destLoc) {
  return routeDistance(currentLoc, destLoc); // It return in meters
}

现在,我需要一个查询,该查询将从 Firebase 返回 10 行,这些行距当前位置的距离最远.

Now, I need a query which will return 10 rows from the firebase which are at the sortest distance from the current location.

我知道,我可以在 firebase 中进行如下查询:

I know, i can query like below in firebase:

  constructor(private firebaseDBProvider: AngularFireDatabase) {
  }

  getDatas(): any {
    return this.firebaseDBProvider.list('data', ref => ref.orderByChild("location").startAt(0).endAt(3));
  }

但是,它不会提供我想要的确切数据.

But, it will not provide me the exact data i want.

我注意到的另一件事是,当我在查询下方运行时,它返回前 10 行.

Another thing, i have noticed is that, when i run below query, it return first 10 rows.

return this.firebaseDBProvider.list('data', ref => ref.limitToFirst(10));

但是,如果要执行以下查询,它会返回 0 行.

But, if want to execute the below query, it returns me 0 rows.

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

这是为什么?startAtendAt 应该与 limitToFirst(10) 相同.

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

那么,查询应该是什么,以便我可以返回距我当前位置排序距离的 10 行?

So, what should be the query so that i can return 10 rows which are the in the sortest distance from my current location?

推荐答案

您的帖子中有两个问题.让我们试着回答每个问题.

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

Q1:我需要一个查询,该查询将从 Firebase 返回 10 行,这些行距当前 locationList 项目的距离最远"

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:如您所知,您将无法在查询时让Firebase 数据库查询引擎"触发您的 calculateDistance() 函数,即 Firebase 只能根据详细的方法进行排序这里 https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data.

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.

换句话说,您将不得不在可以执行您的函数的环境中遍历来自 data 节点的整个数据集:在您的前端或云函数中(例如通过 HTTPS 调用,作为 REST API)

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:为什么会这样?startAt 和 endAt 应该与 limitToFirst(10) 一样工作."

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

A2:不,它们是不同的.

A2: No they are different.

如文档中所述,startAt() 返回大于或等于指定键或值的项目,取决于所选的排序方法."

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

其次,根据 order-by-method,这一行不一定会返回 10 条记录,而是所有 order-by 参数在 0 到 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.

这篇关于如何在 AngularFire2 中按距离当前位置最近的距离排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆