I have a question about filtering a Firestore collection using a function where the values of the documents in the collection are used as arguments. Let's say we have a Firestore collection with documents structured like this:
{
pointOfInterest: "Some string label"
longitude: -100.123
latitude: 50.456
}
In addition, I have code that retrieves a user's geocoordinates (in my case, using react-native), such as:
const getCurrentLatLong = () => {
// do some stuff, then...
return { latitude: someNumber, longitude: someOtherNumber }
}
What I want to achieve is to filter the Firestore collection based on the distance between each document's coordinates and the current user location. Ideally, I would like to do something like this:
let currentLocation = getCurrentLatLong()
let filteredSet = firebase
.firestore()
.collection('MyCollection')
// filtering each individual document
.filter(function (document, currentLocation) {
let docLat = document.latitude
let docLong = document.longitude
return distance(
{latitude: docLat, longitude: docLong},
currentLocation)
< SOME_CONST_DISTANCE
})
This way, I would end up with a filteredSet containing all the documents in the collection that are within a certain distance from the current user's location.
I've done some research and found some potential starting points ( and https://firebase.google.com/docs/reference/js/firebase.database.Query#on), but I'm unsure how to implement what's discussed in those resources. Any advice or documentation on how to accomplish this would be greatly appreciated.