I have data with latitude and longitude stored in my SQLite database, and I want to get the nearest locations to the parameters I put in (ex. My current location - lat/lng,
In order to increase performance as much as possible I suggest improve @Chris Simpson's idea with the following ORDER BY
clause:
ORDER BY ( - * LAT_COL - * LON_COL + LAT_LON_SQ_SUM)
In this case you should pass the following values from code:
= center_lat^2 + center_lon^2
= 2 * center_lat
= 2 * center_lon
And you should also store LAT_LON_SQ_SUM = LAT_COL^2 + LON_COL^2
as additional column in database. Populate it inserting your entities into database. This slightly improves performance while extracting large amount of data.