gpt4 book ai didi

javascript - 使用 Firebase Cloud Functions 访问地理点?

转载 作者:行者123 更新时间:2023-12-03 00:27:43 26 4
gpt4 key购买 nike

我在 Firestore 中有一个名为 posts 的集合,其中包含带有字段 contentlocation 的文档。内容是一个字符串,位置是一个地理点

使用 Firebase Cloud Functions 访问数据库时,我可以通过将内容添加到数组中来打印内容,并将整个内容作为响应 JSON.stringfy

  db.collection("posts").get().then((snapshot) => {
snapshot.forEach((doc) => {

var element = {
"content": doc.data().content,
}
query = query.concat(element);
});
res.send(JSON.stringify(query));

但是,如果我尝试使用地理点来模拟这一点,我只会得到一个空响应 {}

  db.collection("posts").get().then((snapshot) => {
snapshot.forEach((doc) => {
var latitude = doc.data().location.getLatitude();
var longitude = doc.data().location.getLongitude();

var element = {
"latitude": latitude,
"longitude": longitude,
}
query = query.concat(element);
});
res.send(JSON.stringify(query));

如何使用 Javascript 访问 Firestore 中的地理点值,以便我可以对它们执行操作/打印它们?即找到两点之间的距离。

最佳答案

我设法通过使用doc.data().location.latitude直接访问纬度/经度字段来解决这个问题。

关于javascript - 使用 Firebase Cloud Functions 访问地理点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54016879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com