I need help with a function that calculates the route between two points.
public FindClosestPoint(waypoint,currentLocation){
let distances = [];
for(var i=0; i< waypoint.length;i++){
debugger
var rWP1 = new L.Routing.Waypoint;
rWP1.latLng = currentLocation;
var rWP2 = new L.Routing.Waypoint;
rWP2.latLng = waypoint[i];
var myRoute =L.Routing.mapbox('access-token');
myRoute.route([rWP1, rWP2], function(err, routes) {
debugger
var distance = routes[0].summary.totalDistance;
distances.push(distance);
});
}
var minDistance=Math.min.apply(Math, distances)
console.log("distances "+distances);
}
However, I'm encountering an issue where the myRoute.route() function is executing after the FindClosestPoint() function, causing me to not retrieve the distance accurately. I am looking for a way to make myRoute.route() execute inside the for-loop and move on to the next iteration smoothly.
Is there a solution to achieve this? If so, how can it be done? Or are there other alternatives available?