I have a site which shows posts from a certain city. I was thinking of changing from Cities to a radius around the user’s location (10km in this case).
I don’t know how to query using a radius. I can only think of doing a query with two conditions and get a square. e.g:
SELECT fields
FROM points
WHERE lat BETWEEN LAT1 AND LAT2
AND lon BETWEEN LON1 AND LON2
Is it possible to select rows inside a radius of X km/mi from a certain location?
In general, you can use a
WHEREquery on any condition. So, supposing you have a functiondistance()that can compute the distance between two geographic points, you could do:where
city_latandcity_lonis the location of the city centre.PostgreSQL contains a add-on function called
earthdistance()that would help.