我在角度中使用google maps。我需要计算一个起点到许多其他终点之间的距离,但是该函数对所有终点都返回相同的答案。 我该怎么办?
CalculateDistanceBy(AddressStore:string) {let res; this.request.destination = this.tempArrToSaveAddresses [this.mone]; this.mone = this.mone + 1; 返回新的Promise((resolve,reject)=> { 尝试 {this.counter = this.mone; this.directionsService.route(this.request,(response,status)=> {this.request.destination = this.tempArrToSaveAddresses [this.mone-this.counter]; this.counter = this.counter-1; if(status == google.maps.DirectionsStatus.OK){if(this.request.travelMode == google.maps.DirectionsTravelMode.WALKING) {res = response.routes [0] .legs [0] .duration.value; console.log(this.request.destination); } 其他 if(this.request.travelMode == google.maps.DirectionsTravelMode.DRIVING) res = response.routes [0] .legs [0] .distance.text; console.log(“步行距离”,this.response.routes [0] .legs [0] .duration.value); console.log(“ km distance”,this.response.routes [0] .legs [0] .distance.text); console.log(res); 解决(res); } 否则{console.log(“ oops”); res = 0;解决(res); }}); } catch(error){console.log(“ catch”); resolve(0); }})}
tempArrToSaveAddresses:string[]=[];
lengthArr=0;
FindThreeMinPathPerProduct(product1:MinimumPath)
{
let productStores = this.listStoresPerProduct.find(sfp => sfp.Product.productName === product1.product.productName);
if(!productStores) {
return
}
for (let index = 0; index < productStores.Stores.length; index++)
{
this.tempArrToSaveAddresses.push(productStores.Stores[index].addressStore);
this.lengthArr=this.lengthArr+1;
this.CalculateDistanceBy(productStores.Stores[index].addressStore)
.then((res: number) => {
if( res < product1.min1 && res!=0)
{
product1.min1= res;
product1.store1=productStores.Stores[index];
debugger
}
else//|| res < product1.min2
if( res>=product1.min1 && res < product1.min2 && res!=0 )
{
product1.min2= res;
product1.store2=productStores.Stores[index];
debugger
}
else
if( res< product1.min3 && res!=0)
{
product1.min3= res;
product1.store3=productStores.Stores[index];
debugger
}
}, () => {
// reject
})
}
}