首页 > 代码库 > 根据经纬度求两点之间距离(mysql 函数)

根据经纬度求两点之间距离(mysql 函数)

 1 DROP FUNCTION IF EXISTS `getDistance`; 2 DELIMITER ;; 3 CREATE FUNCTION `getDistance`( 4      lon1 float(10,7)  5     ,lat1 float(10,7) 6     ,lon2 float(10,7)  7     ,lat2 float(10,7) 8 ) RETURNS double 9 begin10     declare d double;11     declare radius int;12     set radius = 6378140; 13     set d = (2*ATAN2(SQRT(SIN((lat1-lat2)*PI()/180/2)   14         *SIN((lat1-lat2)*PI()/180/2)+   15         COS(lat2*PI()/180)*COS(lat1*PI()/180)   16         *SIN((lon1-lon2)*PI()/180/2)   17         *SIN((lon1-lon2)*PI()/180/2)),   18         SQRT(1-SIN((lat1-lat2)*PI()/180/2)   19         *SIN((lat1-lat2)*PI()/180/2)   20         +COS(lat2*PI()/180)*COS(lat1*PI()/180)   21         *SIN((lon1-lon2)*PI()/180/2)   22         *SIN((lon1-lon2)*PI()/180/2))))*radius;23     return d;24 end25 ;;26 DELIMITER ;

 

根据经纬度求两点之间距离(mysql 函数)