MySQL计算两个坐标点的距离

-- 定义计算距离的函数
delimiter $
drop function if exists earth_distance$
create function earth_distance ( latitude1 float, longitude1 float, latitude2 float, longitude2 float )
returns float
begin
  set @radius = 6371000;
 set @rad = pi()/180.0;
 set @lat1 = latitude1 * @rad;
 set @lng1 = longitude1 * @rad;
 set @lat2 = latitude2 * @rad;
 set @lng2 = longitude2 * @rad;
 set @theta = @lng2 - @lng1;
 set @dist = acos(sin(@lat1) * sin(@lat2) + cos(@lat1) * cos(@lat2) * cos(@theta));
 return @dist * @radius;
end$

参阅资料

-- 定义计算距离的函数 delimiter $ drop function if exists earth_distance$ create function earth_distance ( latitude1 float, longitude1 float, latitude2 float, longitude2 float ) returns float begin set @radius = 6371000; set @rad = pi()/180.0; set @lat1 = latitude1 * @rad; set @lng1 = longitude1 * @rad; set @lat2 = latitude2 * @rad; set @lng2 = longitude2 * @rad; set @theta = @lng2 - @lng1; set @dist = acos(sin(@lat1) * sin(@lat2) + cos(@lat1) * cos(@lat2) * cos(@theta)); return @dist * @radius; end$ 参阅资料
经验分享 程序员 微信小程序 职场和发展