6
-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.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
DRY Believer
  • 1,001
  • 11
  • 20

2 Answers2

13

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • @Frank this seems like a better answer than my own. Should I delete mine, or do you think there are cases where it might still be useful? – Luke Schlangen Feb 24 '16 at 14:54
  • I thought yours was interesting too, so please leave it. The fact that there's a better/simpler answer, does not make yours invalid. OP might acknowledge that by upvoting it (current upvote is mine). – Frank van Puffelen Feb 24 '16 at 17:48
7

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}

enter image description here

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}

enter image description here

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.

Luke Schlangen
  • 3,722
  • 4
  • 34
  • 69