1

我正在使用 GeoFirestore 在 Cloud Firestore 上编写一个云函数,以根据中心和半径查询附近的位置。

初始化代码如下所示

    const firebase = require('firebase');
    const admin = require('firebase-admin');
    const functions = require('firebase-functions');
    const GeoFirestore = require("geofirestore").GeoFirestore;
    admin.initializeApp(functions.config().firebase);
    const db = admin.firestore();

要查询的JS代码是

const object = db.collection('UserObject');
var geoFirestore = new GeoFirestore(object);
const geoQuery = geoFirestore.query({
    center: new firebase.firestore.GeoPoint(17.500362, 78.587374),
    radius: 1
});
geoQuery.on('ready', () => {
    var locations = geoQuery._locationsTracked;
    var keys = [];
    locations.forEach(function (locationMap, key) {
        if (typeof locationMap !== 'undefined' && locationMap.isInQuery) {
            keys.push(locationMap);
        }
    });
    response.status(200).json(keys);
  });

我是 GeoFirestore 库的新手。我的问题是,这是获取附近位置的正确实现,还是有其他方法可以使用此库查询附近位置?

4

1 回答 1

1

This kind of needs a massive reworking, but the solution will be simpler (code explained in comments):

const object = db.collection('UserObject');
const geoFirestore = new GeoFirestore(object);
const geoQuery = geoFirestore.query({
  center: new firebase.firestore.GeoPoint(17.500362, 78.587374),
  radius: 1
});

// Where we will store the results
const results = [];

// As documents come in, push them into our results
geoQuery.on('key_entered', (key, document) => results.push(document));

// After the query is initially complete we then send the results to the client
geoQuery.on('ready', () => response.status(200).json(results));
于 2018-12-31T05:09:21.920 回答