如何使用async.each方法发送相同集合的两个值?(How to send two values of the same collections with async.each method?)

编程入门 行业动态 更新时间:2024-10-09 07:24:57
如何使用async.each方法发送相同集合的两个值?(How to send two values of the same collections with async.each method?)

我试图使用谷歌距离矩阵api计算n个点之间的距离。

这是代码

var sum=0; for(i=1;i < locations.length;i++){ var origin = locations[i-1]; var destination = locations[i]; var service = new google.maps.DistanceMatrixService; service.getDistanceMatrix({ origins: [origin], destinations: [destination], travelMode: 'DRIVING', unitSystem: google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false }, function(response, status) { if (status !== 'OK') { alert('Error was: ' + status); } else { sum += response.rows[0].elements[0].distance.value; } }); }

但这不起作用。因为我的循环快速完成谷歌距离矩阵响应。 我知道这是错的,它不会起作用。 因此我决定使用我最喜欢的异步库async.js。 但那我该怎么做?

origin = locations[i-1] destination = locations[i].

我只是使用async.each获得该集合的单个元素,这是我选择的方法。 我应该选择async.js的哪种方法来处理这些问题? 或者我应该采用一些完全不同的方法来解决这个问题?

感谢帮助 萨拉斯

编辑 对那些问我为什么要这样做的人? 我想像Uber为自己的应用程序准确计算距离,我正在为此收集大量数据点(实时位置坐标)。 但我不知道他们如何准确地计算距离(另一种方式是方向API,但我发现它并不那么好)。 我只能猜测他们正在使用距离矩阵API并将其应用于旅行中的100多个数据点以计算距离。

I am trying to calculate the distance between n points using the google distance matrix api.

Here is the code

var sum=0; for(i=1;i < locations.length;i++){ var origin = locations[i-1]; var destination = locations[i]; var service = new google.maps.DistanceMatrixService; service.getDistanceMatrix({ origins: [origin], destinations: [destination], travelMode: 'DRIVING', unitSystem: google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false }, function(response, status) { if (status !== 'OK') { alert('Error was: ' + status); } else { sum += response.rows[0].elements[0].distance.value; } }); }

But this doesn't work.As my loop finishes way fast then the google distance matrix response. I know this is wrong and it won't work. Hence I decided to go with my favourite asynchronous library async.js. But then how do I do this?

origin = locations[i-1] destination = locations[i].

I just get a single element of the collection with async.each which was my method of choice. What method of async.js should I choose to deal with such issues? Or should I adopt some entirely different way to work around this problem?

Help is appreciated Saras

EDIT To those who are asking why am I doing this?. I want to calculate distance as accurately as Uber does for its own application and I am collecting a lot of data points(location coordinates real-time) for that. But I don't know how they can calculate the distance that accurately(another way is directions API but I find it not that great). I can only guess they are using the distance matrix API and applying it over 100 data points across a trip to calculate distance.

最满意答案

同步从数组中选择元素并将它们放在另一个数组中:

var legs = []; for (var i=1; i<locations.length; i++) legs.push({origin:locations[i-1], destination:locations[i]});

然后,您可以使用您选择的async.js方法迭代该数组,可能是map或mapSeries ,或者如果您想要做的只是总和距离,可以reduce 。

async.map(legs, function(leg, cb) { service.getDistanceMatrix({ origins: [leg.origin], destinations: [leg.destination], … }, cb); }, function(err, responses) { … })

Select the elements from the array synchronously and put them in another array:

var legs = []; for (var i=1; i<locations.length; i++) legs.push({origin:locations[i-1], destination:locations[i]});

You then can use the async.js method of your choice to iterate that array, probably either map or mapSeries, or maybe reduce if all you want to do is to sum the distances.

async.map(legs, function(leg, cb) { service.getDistanceMatrix({ origins: [leg.origin], destinations: [leg.destination], … }, cb); }, function(err, responses) { … })

更多推荐

本文发布于:2023-04-29 09:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   两个   方法   async   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!