【发布时间】:2020-06-26 17:43:49
【问题描述】:
所以,我从我的 firestore 数据库中获取地址,将它们转换为坐标,然后尝试根据每组坐标添加标记。我可以在循环外添加标记,但不能在循环内添加。
谢谢
//Convert addresses to coords
//Fetch all addresses from db
db.collection('specials').get()
.then(snapshot => {
snapshot.forEach(special => {
//Set location
var location = special.data().address;
//Get geocode info
axios.get('https://maps.googleapis.com/maps/api/geocode/json?components=country:NZ|country:AU',{
params:{
address:location,
key:'************************************',
}
})
.then(function(response){
//Geometry
markerLat = response.data.results[0].geometry.location.lat;
markerLng = response.data.results[0].geometry.location.lng;
//console.log("Lat:" + markerLat + " Lng:" + markerLng);
//Doesn't work inside the loop
markers.push({
coords:{lat: markerLat, lng: markerLng}
});
})
});
})
//Works outside the loop
markers.push({
coords:{lat: -36.8585, lng: 174.7833}
});
【问题讨论】:
-
你能澄清你的问题吗?当您说它“在循环内不起作用”时,您是什么意思?你如何测试它?您确定要考虑地理编码器的异步性质吗?请提供一个 minimal reproducible example 来证明您的问题。
-
@James:我的回答有什么帮助吗?你有进步吗?
标签: javascript arrays google-maps google-cloud-firestore