Я использую плагин 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 - глобальная переменная в моем коде. могу я не знать, где проблема