使用firebase找到附近的latlng

DRY*_*ver 6 firebase firebase-realtime-database

-uniqueid1
            latitude: 30.7188235
            longitude: 76.810132
-uniqueid2
            latitude: 30.7188235
            longitude: 76.810132
Run Code Online (Sandbox Code Playgroud)

firebase中有1000个这样的记录,我想找到最接近特定long/lat的唯一ID.我开始使用startAt()和endAt()但没有成功.我们如何实现条件子句和多个查询.查询我寻找是

SELECT uniqueids from table where (lon-specific_lon)^2+(lat-specific_lat)^2<CONSTANT.
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 13

您正在寻找的是Firebase的Geofire库的一部分.它使用Geohashes巧妙地解决Firebase的限制,您只能查询单个值.

Geohashes是一种将经度和纬度填充到适合范围查询的单个值的方法.

有关更多信息,请参阅GeofireGithub回购.


Luk*_*gen 7

我不相信有一种内置的方法可以做到这一点,但一种方法可能是创建一个方形网格并将每个位置分配给一个区域 - 比如说,区域

{x: 2, y: 4}

在此输入图像描述

然后,您可以通过返回可在数据调用中调整的特定范围内的所有内容来返回区域和邻居区域的所有位置.例如,如果您想返回1个区域内的所有内容{x: 2, y: 4},您将返回:

{x: 1, y: 3}
{x: 1, y: 4}
{x: 1, y: 5}
{x: 2, y: 3}
{x: 2, y: 4} // The region you're in right now
{x: 2, y: 5}
{x: 3, y: 3}
{x: 3, y: 4}
{x: 3, y: 5}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这将返回您所在区域周围的正方形以及该正方形中的所有位置.如果您需要它是圆形的,那么您可以在前端修剪您的选择.这不是一个完美的解决方案,但它可能是一种方法来做我认为你想要完成的事情,这是返回更少的数据.