我想做的是,当用户输入位置的经度和纬度时,应用程序返回关联的网格代码,该代码存储在3个json数据文件之一中。我正在尝试同时搜索所有三个文件,这是我的尝试,但smes无法正常工作。
async function lookupGridcodeByLatAndLong(lat, long){
const response=await fetch('data/GHI_part01.json');
const line=await response.json();
var result = line.find(x => x.lat == lat && x.long == long);
if (result) {
return result.GRIDCODE;
} else {
lookupGHI_part02(lat, long);
}
}
async function lookupGHI_part02(lat, long){
const response=await fetch('data/GHI_part02.json');
const line=await response.json();
var result = line.find(x => x.lat == lat && x.long == long);
if (result) {
return result.GRIDCODE;
} else {
lookupGHI_part03(lat, long);
}
}
async function lookupGHI_part03(lat, long){
const response=await fetch('data/GHI_part03.json');
const line=await response.json();
var result = line.find(x => x.lat == lat && x.long == long);
if (result) {
return result.GRIDCODE;
} else {
return new Error('No records found');
}
}
$("#Btn" ).click(function() {
lookupGridcodeByLatAndLong(lat, long)
.then(gridcode => {
var ghi = gridcode;})
$("#result").show();
$("#resultat").html(ghi);
}
这是我的数据的样子
[{"GRIDCODE":2115,"Lat":29.478,"Long":-5.565},{"GRIDCODE":2115,"Lat":29.478,"Long":-5.562},{"GRIDCODE":2115,"Lat":29.486,"Long":-5.572},{"GRIDCODE":2113,"Lat":29.483,"Long":-5.567},{"GRIDCODE":2109,"Lat":29.483,"Long":-5.559},{"GRIDCODE":2115,"Lat":29.485,"Long":-5.554},{"GRIDCODE":2115,"Lat":29.493,"Long":-5.616},{"GRIDCODE":2120,"Lat":29.492,"Long":-5.609},{"GRIDCODE":2116,"Lat":29.492,"Long":-5.601},{"GRIDCODE":2116,"Lat":29.492,"Long":-5.592},{"GRIDCODE":2115,"Lat":29.493,"Long":-5.584},{"GRIDCODE":2110,"Lat":29.491,"Long":-5.575}]
第一个错误是在
var result = line.find(x => x.Lat == lat && x.Long == long);
我在x.Lat
和x.Long
中更改为大写的“ L”
在另一个函数中的第二个调用函数实例,我将其更改为此
async function lookupGridcodeByLatAndLong(lat, long){
const response=await fetch('data/GHI_part01.json');
const line=await response.json();
var check =0;
var result = line.find(x => x.Lat == lat && x.Long == long);
if (result) {
return result.GRIDCODE;
}
else {
var check = 1;
}
if (check == 1)
{
const response=await fetch('data/GHI_part02.json');
const line=await response.json();
var result = line.find(x => x.Lat == lat && x.Long == long);
if (result) {
return result.GRIDCODE;
} else {
check = 2
}
}
if (check = 2)
{
const response=await fetch('data/GHI_part03.json');
const line=await response.json();
var result = line.find(x => x.Lat == lat && x.Long == long);
if (result) {
return result.GRIDCODE;
} else {
return new Error('No records found');
}
}
}