1、第一种方式,通过st_distance函数计算出距离,这个函数必须是php5.6及以上mysql版本才能支持,具体函数的方法,大家自己查查
下边是一个小案例,查询经纬度(lng2,lat2)距离这个点一公里范围内匹配的数据取出前100条,可以根据自己的情况直接复制代码测试,亲测有效
SELECT company_name,lng1,lat1,
(st_distance(point(lng1, lat1),point(lng2, lat2))*111195) AS distance
FROM track
HAVING distance<1000
ORDER BY distance
limit 100
2、第二种是创建函数然后调用
drop function if exists Spherical_Distance;
create function Spherical_Distance(jin1 double,wei1 double,jin2 double,wei2 double) returns double
NO SQL
BEGIN
declare j1 double;
declare w1 double;
declare j2 double;
declare w2 double;
declare R double;
set j1 = jin1*PI()/180;
set w1 = wei1*PI()/180;
set j2 = jin2*PI()/180;
set w2 = wei2*PI()/180;
set R = 6370986;
return R*acos(cos(w1)*cos(w2)*cos(j1-j2)+sin(w1)*sin(w2));
END