2013-04-02 134 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