sqlpostgresqlgpsdistanceearthdistance

Postgres, earthdistance - sort results by gps distance


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?


Solution

  • 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.'
       )
    ';