Finding all rows within a radius in SQLite
Where :lat and :lng are the latitude and longitude of the point of interest, and :radius is the radius in kilometres:
SELECT *,
(6371 * 2 *
ATAN2(
SQRT(
POW(SIN(RADIANS((CAST(:lat AS REAL) - CAST(latitude AS REAL)) / 2)), 2) +
COS(RADIANS(CAST(:lat AS REAL))) * COS(RADIANS(CAST(latitude AS REAL))) *
POW(SIN(RADIANS((CAST(:lng AS REAL) - CAST(longitude AS REAL)) / 2)), 2)
),
SQRT(1 - (
POW(SIN(RADIANS((CAST(:lat AS REAL) - CAST(latitude AS REAL)) / 2)), 2) +
COS(RADIANS(CAST(:lat AS REAL))) * COS(RADIANS(CAST(latitude AS REAL))) *
POW(SIN(RADIANS((CAST(:lng AS REAL) - CAST(longitude AS REAL)) / 2)), 2)
))
)
) AS distance
FROM places
WHERE distance < CAST(:radius AS REAL)
ORDER BY distance
LIMIT 50