Finding nearby latlng using firebase

时光怂恿深爱的人放手 提交于 2021-02-04 15:27:28

问题


-uniqueid1
            latitude: 30.7188235
            longitude: 76.810132
-uniqueid2
            latitude: 30.7188235
            longitude: 76.810132

there are 1000 such records in firebase , I want to find uniqueids closest to a specific long/lat.I started using startAt() and endAt() but no success.How do we implement conditional clause and multiple queries.The query me looking for is

SELECT uniqueids from table where (lon-specific_lon)^2+(lat-specific_lat)^2<CONSTANT.

回答1:


What you're looking for is part of Firebase's Geofire library. It uses Geohashes to cleverly work around Firebase's limit that you can only query on a single value.

Geohashes are a way to stuff longitude and latitude into a single value that is suitable for range querying.

See the Github repo for Geofire for more information.




回答2:


I don't believe there is a built-in way to do this, but one way might be to create a square grid and assign each location to a region - say, region

{x: 2, y: 4}

Then you could return all of the locations that region and neighbor regions by returning everything in a certain range that could be adjusted in your data call. For example, if you wanted to return everything within 1 region of {x: 2, y: 4}, you would return:

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

This would return a square surrounding your region and all of the locations in that square. If you need it to be circular, you could then trim your selection on the front end. It's not a perfect solution, but it might be a way to do what I think you're trying to accomplish, which is return less data.



来源:https://stackoverflow.com/questions/35584667/finding-nearby-latlng-using-firebase

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