Axel Stone
Axel Stone

Reputation: 1580

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?

Upvotes: 0

Views: 758

Answers (1)

Vao Tsun
Vao Tsun

Reputation: 51609

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

Upvotes: 1

Related Questions