-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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With