gpt4 book ai didi

javascript - 使用 GEOFirestore 获取附近的 GEO 结果

转载 作者:行者123 更新时间:2023-11-30 19:56:02 25 4
gpt4 key购买 nike

我正在使用 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 库的新手。我的问题是,这是获取附近位置的正确实现方式,还是有其他方法可以使用此库查询附近位置?

最佳答案

这种需要大量返工,但解决方案会更简单(代码在评论中解释):

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

关于javascript - 使用 GEOFirestore 获取附近的 GEO 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53983550/

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