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
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.