mysql 计算经度纬度之间的距离

DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `getDistance`(lng1 float, lat1 float, lng2 float, lat2 float) RETURNS double
BEGIN
DECLARE result double;

DECLARE radLng1 double;
DECLARE radLat1 double;

DECLARE radLng2 double;
DECLARE radLat2 double;

set radLng1 = lng1 * pi() / 180.0;
set radLat1 = lat1 * pi() / 180.0;

set radLng2 = lng2 * pi() / 180.0;
set radLat2 = lat2 * pi() / 180.0;

set result = 2 * asin(sqrt(pow(sin((radLat1 - radLat2) / 2), 2) + cos(radLat1) * cos(radLat2) * pow(sin((radLng1 - radLng2) / 2), 2)));
set result = result * 6378.137;
set result = round(result * 100) / 100;
RETURN result;
END$$
DELIMITER ;

最后返回小位小数

 

posted @ 2019-07-01 15:23  贵哥好懒  阅读(602)  评论(0编辑  收藏  举报