For my application, I've an User where each one is geolocated. What I want to achieve, is to retrieve User who are within a distance (e.g. User where distance >= 1000 and distance <= 2000).
My user have a longitude
and a latitude
field.
So far, here's my request :
select(%{users.*,
ST_DISTANCE(
ST_GeographyFromText(
'SRID=4326;POINT(' || users.longitude || ' ' || users.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)')
) as distance
} % [longitude, latitude])
.where(%{
ST_DISTANCE(
ST_GeographyFromText(
'SRID=4326;POINT(' || users.longitude || ' ' || users.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)')
) > %d AND
ST_DWithin(
ST_GeographyFromText(
'SRID=4326;POINT(' || users.longitude || ' ' || users.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
%d
)
} % [longitude, latitude, 15, longitude, latitude, distance_in_meters])
This is pretty ugly : can you help me ?