I am attempting to get the distance between to lat|long coordinates by using the npm geolib. I have a function in which I query firestore and get an array of objects back. I then run that array through a for loop where I call geolib.getPreciseDistance to get the distance for each object of the array.
I am having a hard time getting a sorted array in return from the for loop. In a perfect world the new array would be sorted with the object with the closest distance in index[0].
I've posted as far as I have got below. I appreciate any and all feedback as I am still learning. Thanks!
function getNearestWalk() {
let currentLng = userLocation.longitude;
let currentLat = userLocation.latitude;
firestore()
.collection('Walks')
.where('status', '==', 'started')
.limit(10)
.get()
.then((walks) => {
const walksArray = [];
walks.forEach((liveWalks) => {
walksArray.push(liveWalks.data());
});
console.log(walksArray);
for (var i = 0; i < walksArray.length; i++) {
geolib.getPreciseDistance(
{ latitude: currentLat, longitude: currentLng },
{
latitude: walksArray[i].latitude,
longitude: walksArray[i].longitude,
}
);
}
});
}
geolib.getPreciseDistance(), please edit your question and add it, you can doconsole.log(geolib.getPreciseDistance({//put parameters here},{//put parameter here}))