数组的数据位置错误

时间:2019-02-25 16:30:30

标签: ionic-framework ionic3

我有下面的函数,该函数在循环中调用其他函数,但是每次我运行它时,都会得到数组中位置错误的数据。也许我必须等待完成forwardGeocode才能完成,然后循环继续进行下去?如果是的话,我该怎么做?

News() {
this.restProvider.getNews()
.then(data => {
   this.NewsDetails = data;
   this.date = this.NewsDetails[1].date;
   this.day = this.NewsDetails[1].day;
  for(let i=0;i<this.NewsDetails.length;i++){
    this.full_address = this.NewsDetails[i].address + ', ' + this.NewsDetails[i].town;
    this.forwardGeocode(this.full_address);
    console.log(this.full_address);

  }
});
console.log(this.full_address);

forwardGeocode(keyword) 
{
    this._GEOCODE.forwardGeocode(keyword)
    .then((coordinates : NativeGeocoderForwardResult[]) =>
    {
       let str : string   = `The coordinates are latitude=${coordinates[0].latitude} and longitude=${coordinates[0].longitude}`;
       //console.log(str);
       this.results       = str;
       this.cords.push(str);
       this.plat = coordinates[0].latitude;
       this.plong = coordinates[0].longitude;

       console.log(this.cords);
       console.log(this.mylat,this.plat,this.mylongt,this.plong,'alo4');

       this.calculateDistance(this.mylat,this.plat,this.mylongt,this.plong);


    })
    .catch((error: any) =>
    {
       console.log(error);

    });

  }
  }

0 个答案:

没有答案