首页 > 解决方案 > 从地址获取纬度和经度无法正常工作

问题描述

我使用 nativegeocoder 插件来获取纬度和经度。它工作完美的单一输入。它不能完美地循环。

    this.JobsVals = [];   this.database.selectjobsvaltable().then((data) => {
    var item = {} as any;

    item = data;
    this.items = item;
    if (this.items != null && this.items.length > 0) {
      for (var index = 0; index < this.items.length; index++) {
        if (this.items[index].checkedvalues == "1" || this.items[index].checkedvalues == "1.0") {
          this.items[index].checkedvalues = "1";
          for(let i = index; i< this.data_obj.length;i++){

          let address = this.data_obj[i].addr +this.data_obj[i].addr1+','+this.data_obj[i].city+','+this.data_obj[i].state;

          this.getAddress(address);
          let addlat = {'name':this.data_obj[i].fullname,'address':address,'lat':this.global.lat,'lon':this.global.lon,'jival':this.data_obj[i].jivalue};
          this.locationArray.push(addlat);
            console.log(this.locationArray);
            break;
          }
          this.job = {
            agencyid: this.items[index].agencyID,
            cvalue: this.items[index].cvalue,
            caseno: this.items[index].caseno,
            cco: this.items[index].cco,
            dvalue: this.items[index].dvalue,
            documents: this.items[index].documents,
            dohttppost: this.items[index].dohttppost,
            dohttppostip: this.items[index].dohttppostip,
            email: this.items[index].email,
            emailphotoid: this.items[index].emailphotoid,
            fullname: this.items[index].fullname,
            jivalue: this.items[index].jivalue,
            lawfirmjobref: this.items[index].lawfirmjobref,
            pvalue: this.items[index].pvalue,
            subserverid: this.items[index].subserverid,
            checkedvalues: "1"

          };
          this.storage.set("mapArray",JSON.stringify(this.locationArray));
          console.log("Map Array :"+this.storage.get("mapArray"))
          this.JobsVals.push(this.job);
          localStorage.setItem("jobsarray", JSON.stringify(this.items));
        }
        if(index == this.items.length-1){
          localStorage.setItem("selectedjobsarray", JSON.stringify(this.JobsVals));
          this.navCtrl.push(MapPage,this.locationArray);
        }
      }
    } else {

      this.navCtrl.push(MapPage, this.locationArray);
    }   }, (error) => {   });


getAddress(address){   let options: NativeGeocoderOptions = {
    useLocale: true,
    maxResults: 5 };   this.nativeGeocoder.forwardGeocode(address, options)   .then((coordinates: NativeGeocoderForwardResult[]) =>{   this.global.lat = coordinates[0].latitude;   this.global.lon = coordinates[0].longitude;

  console.log("lat:"+this.global.lat);   console.log("lon:"+this.global.lon);  }) .catch((error: any) => console.log(error)); }

如果我设置延迟方法,我将获得所有地址的最后一个地址 lat,lon。否则它显示未定义。我在我的代码中声明 lat 和 lon 是全局变量。我可以不知道问题出在哪里吗

标签: javascriptangularjsionic3

解决方案


推荐阅读