I have a SQLite database in Android with a table that contains longitude, latitude and other columns.. now I want to get all records that have the distance smaller or equal to a given distance.
For example, I know my current coordinates(lat,long) and I want to get from my table all records that are at the maximum distance of 10km from me.
I found some links on stack but nothing too solid. Is there someone that knows an optimized solution for this problem?
I have thought that I could get all records that have latitude smaller than my lat + distance and greater than my lat – distance and longitude smaller than my long + distance and greater than long – distance. After this query I should check for some unwanted records since the query is not returning only the wanted records..
Is this a good idea?
You probably want to do this in two parts,
1) Run a query where you find all records that are within a certain value + or – of the current lat/lng of your location, the where clause might look like:
where (@latitude > (lat – .001) and @latitude > (lat – .001)) and
(@longitude> (lng- .001) and @longitude> (longitude- .001))
2) with the rough results from above, use the great circle/haversine method to determine what the actual distance between each location is (great circle/haversinse is already part of the android maps api).