Query Location by Radius with GeoFire

依然范特西╮ 提交于 2019-12-04 20:02:23

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").

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

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]
        }
    }
}

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!