我有一个计算两点之间路线的函数。
public ClosestPoint(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);
}
但我面临的问题是myRoute.route( )函数在ClosestPoint()执行之后执行,所以我无法正确获取距离。我希望myRoute.route()在 for 循环内执行,然后开始下一次迭代。
是否可以?如果是,那怎么办?或者它还有其他解决方案吗?