简体   繁体   English

使用Firebase Cloud功能访问Geopoint?

[英]Accessing Geopoints using Firebase Cloud Functions?

I have a collection called posts in Firestore which contains a documents with the fields content and location . 我在Firestore中有一个称为posts的集合,其中包含带有字段contentlocation的文档。 Content is a string and Location is a Geopoint . 内容是一个string ,位置是一个Geopoint

When accessing the database using Firebase Cloud Functions, I am able to print out the content by adding it into an array and JSON.stringfy the whole thing as a response. 当使用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));

However, If i try to emulate this with the geopoints, I simply get an empty response {} . 但是,如果我尝试使用geopoints进行仿真,则只会得到一个空的响应{}

  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));

How do access the Geopoint values in the Firestore using Javascript so I can perform operations on/print them? 如何使用Javascript在Firestore中访问Geopoint值,以便可以对它们执行操作/打印它们? ie find distance between two points. 即找到两点之间的距离。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM