MYSQL求经纬度之间的距离

CREATE DEFINER=`root`@`%` FUNCTION `getDistance`(`lat1` decimal(10,7),`lon1` decimal(10,7),`lat2` decimal(10,7),`lon2` decimal(10,7)) RETURNS decimal(10,2)
BEGIN
    #Routine body goes here...
 DECLARE dis double;
    SET dis = acos(sin(lat1 * 3.1415 / 180) * sin(lat2 * 3.1415 / 180) + cos(lat1 * 3.1415 / 180) * cos(lat2 * 3.1415 / 180) * cos(lon1 * 3.1415 / 180 - lon2 * 3.1415 / 180)) * 6378.138;
    RETURN dis;
END

注意:

1.创建函数用户的有权限,没权限无法创建

2.参数小数点后位数指定,否则经纬度相近距离为0

posted @ 2020-12-23 17:39  感情就是后人乘凉  阅读(118)  评论(0编辑  收藏  举报