写这段代码的时候
var holder = [];
const compile = () =>{
let latitude = 0;
let longitude = 0;
for (let i = 0; i < holder.length; i++) {
Geocode.fromAddress(holder[i].city).then(
(response) => {
const { lat, lng } = response.results[0].geometry.location;
const foo = {lat, lng}
longitude += Number(foo.lng);
console.log(longitude)
latitude += Number(foo.lat);
console.log(latitude)
},
(error) => {
console.error(error);
}
);
}
console.log("lat: " + latitude)
latitude = latitude/holder.length;
longitude = longitude/holder.length;
console.log("lattitude: ", latitude, " longitude: ", longitude);
}
输出看起来像这样
lat: 0 AddBox.js:49
lattitude: 0 longitude: 0 AddBox.js:53
-118.2436849 AddBox.js:39
34.0522342 AddBox.js:41
-192.7821738 AddBox.js:39
74.62067049999999
为什么在 for 循环运行之前打印 console.log?
我正在尝试使用地理编码对几个位置的经度和纬度进行平均,但代码似乎并没有从上到下运行