当前位置:Gxlcms > 数据库问题 > mysql实现经纬度计算两个坐标之间的距离

mysql实现经纬度计算两个坐标之间的距离

时间:2021-07-01 10:21:17 帮助过:10人阅读

DELIMITER $$
CREATE DEFINER = CURRENT_USER FUNCTION `getDistance`(`lon1` float,`lat1` float,`lon2` float,`lat2` float)
RETURNS double
begin
declare d double;
declare radius int;
set radius = 6378140; #假设地球为正球形,直径为6378140米
set d = (2*ATAN2(SQRT(SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)+
COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lon1-lon2)*PI()/180/2)
*SIN((lon1-lon2)*PI()/180/2)),
SQRT(1-SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)
+COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lon1-lon2)*PI()/180/2)
*SIN((lon1-lon2)*PI()/180/2))))*radius;
return d;
END $$
DELIMITER $$;

mysql实现经纬度计算两个坐标之间的距离

标签:sql   set   经纬   def   float   body   ble   fine   func   

人气教程排行