如何在 AngularFire2 中按离当前位置最近的距离排序?
How to orderBy the closest distance from current location in AngularFire2?
我正在使用 AngularFire2 库在 firebase 中进行查询。在我的 firebase 中,我有 data 集合,在它们中,我有很多行 latitude
和 longitude
,如下所示:
- data
- 1404033346513076_1998422263740845
- location
- latitude: 23.7907795
- longitude: 90.403898
- 1404033346513076_1998422263740888
- location
- latitude: 23.9907795
- longitude: 91.403898
我有我当前位置的 latitude
和 longitude
,我有一个辅助函数,可以确定当前位置与 firebase 数据中的位置之间的距离(以米为单位)。假设,函数如下所示:
calculateDistance(currentLoc, destLoc) {
return routeDistance(currentLoc, destLoc); // It return in meters
}
现在,我需要一个查询,该查询将 return 来自 firebase 的 10 行,这些行与当前位置的距离最短。
我知道,我可以在 firebase 中像下面这样查询:
constructor(private firebaseDBProvider: AngularFireDatabase) {
}
getDatas(): any {
return this.firebaseDBProvider.list('data', ref => ref.orderByChild("location").startAt(0).endAt(3));
}
但是,它不会提供我想要的确切数据。
另一件事,我注意到,当我 运行 下面查询时,它 return 前 10 行。
return this.firebaseDBProvider.list('data', ref => ref.limitToFirst(10));
但是,如果要执行下面的查询,它 return 我 0 行。
this.firebaseDBProvider.list('data', ref => ref.startAt(0).endAt(10));
这是为什么? startAt 和 endAt 应该与 limitToFirst(10).
相同
那么,查询应该是什么,以便我可以 return 10 行,这些行距离我当前位置最短距离?
你的 post 中有两个问题。让我们尝试回答每个问题。
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 database querying engine" 触发您的 calculateDistance()
函数,即 Firebase 只能根据此处详述的方法进行排序 https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data.
换句话说,您必须在可以执行您的功能的环境中循环 data
节点的整个数据集:在您的前端或可能在 Cloud Function 中(例如由 HTTPS 调用,作为 REST API)
Q2:"Why is that? startAt and endAt should work same as the limitToFirst(10)."
A2:不,它们不一样。
如文档中所述,"startAt() returns items greater than or equal to the specified key or value, depending on the order-by method chosen."
因此,首先,您必须在这一行包含按方法排序
this.firebaseDBProvider.list('data', ref => ref.startAt(0).endAt(10));
其次,根据排序方式,这一行不一定是 return 10 条记录,而是排序参数在 0 到 10 之间的所有记录。
我认为,使用GeoFire 可以是一种按最近距离获取数据顺序的解决方案。
我正在使用 AngularFire2 库在 firebase 中进行查询。在我的 firebase 中,我有 data 集合,在它们中,我有很多行 latitude
和 longitude
,如下所示:
- data
- 1404033346513076_1998422263740845
- location
- latitude: 23.7907795
- longitude: 90.403898
- 1404033346513076_1998422263740888
- location
- latitude: 23.9907795
- longitude: 91.403898
我有我当前位置的 latitude
和 longitude
,我有一个辅助函数,可以确定当前位置与 firebase 数据中的位置之间的距离(以米为单位)。假设,函数如下所示:
calculateDistance(currentLoc, destLoc) {
return routeDistance(currentLoc, destLoc); // It return in meters
}
现在,我需要一个查询,该查询将 return 来自 firebase 的 10 行,这些行与当前位置的距离最短。
我知道,我可以在 firebase 中像下面这样查询:
constructor(private firebaseDBProvider: AngularFireDatabase) {
}
getDatas(): any {
return this.firebaseDBProvider.list('data', ref => ref.orderByChild("location").startAt(0).endAt(3));
}
但是,它不会提供我想要的确切数据。
另一件事,我注意到,当我 运行 下面查询时,它 return 前 10 行。
return this.firebaseDBProvider.list('data', ref => ref.limitToFirst(10));
但是,如果要执行下面的查询,它 return 我 0 行。
this.firebaseDBProvider.list('data', ref => ref.startAt(0).endAt(10));
这是为什么? startAt 和 endAt 应该与 limitToFirst(10).
相同那么,查询应该是什么,以便我可以 return 10 行,这些行距离我当前位置最短距离?
你的 post 中有两个问题。让我们尝试回答每个问题。
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 database querying engine" 触发您的 calculateDistance()
函数,即 Firebase 只能根据此处详述的方法进行排序 https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data.
换句话说,您必须在可以执行您的功能的环境中循环 data
节点的整个数据集:在您的前端或可能在 Cloud Function 中(例如由 HTTPS 调用,作为 REST API)
Q2:"Why is that? startAt and endAt should work same as the limitToFirst(10)."
A2:不,它们不一样。
如文档中所述,"startAt() returns items greater than or equal to the specified key or value, depending on the order-by method chosen."
因此,首先,您必须在这一行包含按方法排序
this.firebaseDBProvider.list('data', ref => ref.startAt(0).endAt(10));
其次,根据排序方式,这一行不一定是 return 10 条记录,而是排序参数在 0 到 10 之间的所有记录。
我认为,使用GeoFire 可以是一种按最近距离获取数据顺序的解决方案。