如何订购距离AngularFire2当前位置最近的距离? [英] How to orderBy the closest distance from current location in AngularFire2?

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

问题描述

我使用 AngularFire2 库在firebase中进行查询。在我的firebase中,我有数据集合,在其中,我有很多行纬度经度如下所示:

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

我有纬度经度我当前的位置,我有一个辅助功能,可以确定当前位置和火力基础数据的位置之间的距离。假设,函数如下所示:

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
}

现在,我需要一个返回10的查询来自firebase的行与当前位置的最近距离。

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

为什么? startAt endAt 应与 limitToFirst(10)相同。

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

所以什么应该是查询,以便我可以从当前位置返回10行 sortest distance

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

其次,根据逐个方法,这条线不一定会返回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天全站免登陆