2013-04-02 128 views
0

我有一个包含GPS坐标经度和纬度的mysql表。查找接近距离GPS

我想提出一个请求,以获得一个给定位置附近的记录。

我已经试过2个请求和合并的结果,但它不是正确的......

//Req 1 - bigger than my position 
$req1 = mysql_query('SELECT * FROM table WHERE latitude >= 47.6621549 and longitude >= 2.3547128 ORDER BY latitude, longitude'); 

//Req 2 - samller than my position 
$req2 = mysql_query('SELECT * FROM table WHERE latitude <= 47.6621549 and longitude <= 2.3547128 ORDER BY latitude, longitude'); 

是否有可能有一个请求,使其? 有了这个,我想通过gmap distance API检索给定位置的距离,我认为这不是问题...

感谢您的回答。

+1

这个答案可能会帮助您:http://stackoverflow.com/questions/1006654/fastest-way-to-find-distance-between-two-lat-long-points – shannonman

+0

这很可能是相关的你的问题http://dba.stackexchange.com/questions/4214/how-to-best-implement-nearest-neighbour-search-in-mysql –

回答

2

试试这个。

$range = 2;/* 2 mi */ 
mysql_query("SELECT t1.*, 
    (
     3956 * 2 * 
     ASIN(
      SQRT(
       POWER(SIN(($lat - t1.latitude) * pi()/180/2), 2) 
       + COS($lat * pi()/180) * COS(t1.latitude * pi()/180) 
       * POWER(SIN(($lng - t1.longitude) * pi()/180/2), 2) 
      ) 
     ) 
    ) AS distance 
    FROM table 
    HAVING distance < $range 
    ORDER BY distance ASC"); 
+0

非常感谢!如果您想要知识管理系统,请在KM – jlafforgue

+0

@jlafforgue上加上乘法运算,然后用'6371'代替'3956'。 –

+0

这是金币 - 谢谢 – case1352