找到接近距离GPS

时间:2013-04-02 08:35:39

标签: mysql gps request location coordinates

我有一个包含GPS坐标纬度和经度的mysql表。

我想请求在给定位置附近获取记录。

我已经尝试了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 个答案:

答案 0 :(得分: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");