问题
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
}
Now, I need a query which will return 10 rows from the firebase which are at the sortest distance from the current location.
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.
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));
But, if want to execute the below query, it returns me 0 rows.
this.firebaseDBProvider.list('data', ref => ref.startAt(0).endAt(10));
Why is that? startAt and endAt should work same as the limitToFirst(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?
回答1:
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.
回答2:
I think, using GeoFire can be a solution to get the data order by closest distance.
来源:https://stackoverflow.com/questions/50112214/how-to-orderby-the-closest-distance-from-current-location-in-angularfire2