Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding nearby latlng using firebase

-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.
like image 842
DRY Believer Avatar asked Feb 23 '16 17:02

DRY Believer


2 Answers

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.

like image 185
Frank van Puffelen Avatar answered Oct 25 '22 07:10

Frank van Puffelen


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.

like image 33
Luke Schlangen Avatar answered Oct 25 '22 09:10

Luke Schlangen