本文介绍了使用GeoFire通过Radius查询位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个基本问题,但是我仍然遇到很多麻烦.

I know this is a basic question, but I’m having a lot of trouble with it nonetheless.

我有一个存储社区事件的Firebase数据库.每个事件节点都有一个地理位置节点(使用GeoFire创建),名为 eventPlace (请参见下面的屏幕截图).

I have a Firebase database storing community events. Each event node has a geo location node (created with GeoFire) called eventPlace (see screenshot below).

使用GeoFire(和javascript),如何查询整个数据库并获取特定位置/半径内的所有事件?考虑到数据的存储方式,这可能吗?还是我需要将所有位置节点移动到一个公共节点(eventPlaces ??)并查询该单个父节点?

Using GeoFire (and javascript), how would I query the entire database and get all events within a certain location/radius? Is this possible, given the way the data is stored? Or do I need to move all the location nodes to a common node (eventPlaces??) and query that single parent node?

请注意,我并不是在寻找实时数据.这些位置是以前存储的,不会经常更改.

Note that I am not seeking real-time data. These locations are stored previously and don’t change very often.

预先感谢...

推荐答案

目前而言,geofire用作索引,用于进行地理查询,并提供所需文档的密钥(该文档将存储在此文档中)在单独的集合"中.

As it stands right now geofire sort of serves as an index to make geoqueries on, and provides the key of the document you want (which would be stored in a separate "collection").

您应该使用geofire和一个单独的集合"(将其命名为 eventPlaces )

You should be using geofire and a separate "collection" (call it eventPlaces)

var firebaseRef = firebase.database().ref('eventPlaces');
var geoFire = new GeoFire(firebaseRef);

现在,您可以将其用作事件的索引,并可以向其中添加项目.

Now you can use it as an index for your events, and can add items to it like so.

geoFire.set('-K_Pp-3RBJ58VkHGsL5P', [40.607765, -73.758949]);

您的Firebase RTDB现在将如下所示:

Your Firebase RTDB will look like this now:

{
   'events': {
        '-K_Pp-3RBJ58VkHGsL5P': {
            // All your data here
        }
    },
   'eventPlaces': {
        '-K_Pp-3RBJ58VkHGsL5P': {
            'g': 'dr5x186m7u',
            'l': [40.607765, -73.758949]
        }
    }
}

最后,当您对geoFire进行查询时:

So finally when you do a query on your geoFire:

geoFire.query({
  center: [40.607765, -73.758949],
  radius: 10
}).on('key_entered', (key, location, distance) => {
  console.log(key + ' entered query at ' + location + ' (' + distance + ' km from center)');
});

您最终将获得该文档的密钥,为此您可以对该单个文档进行常规的Firebase查询.

You'll end up being returned the key of the doc, for which you can do a normal Firebase query for that individual doc.

这篇关于使用GeoFire通过Radius查询位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 11:33