Postgres, earthdistance - sort results by gps distance

709 Views Asked by At

Currently I have a query that selects all the objects closer than X km from specified GPS coordinations:

$query = 
  'SELECT * FROM t_activity WHERE
   POINT(gps_lat||\',\'||gps_lon) <@> 
   POINT(
     '.$gps_latitude.', 
     '.$gps_longitude.'
   ) <= '.$search_range;

Im not skilled in DBs at all, but I would like to know:

Is it also possible to modify the query in order to sort it from closest to farest results?

1

There are 1 best solutions below

0
Vao Tsun On BEST ANSWER

from your query I assume it should be

$query = 
  'SELECT * FROM t_activity WHERE
   POINT(gps_lat||\',\'||gps_lon) <@> 
   POINT(
     '.$gps_latitude.', 
     '.$gps_longitude.'
   ) <= '.$search_range.'
   ORDER BY POINT(gps_lat||\',\'||gps_lon) <@> 
   POINT(
     '.$gps_latitude.', 
     '.$gps_longitude.'
   )
';