How to orderBy the closest distance from current location in AngularFire2?


#1

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?