我有一个 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 检索到给定位置的距离,我认为这不是问题......
感谢您的回答。
最佳答案
试试这个。
$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");
关于mysql - 查找接近距离 GPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15759518/