SQLSERVER 根据地图经纬度计算距离差示例
程序员文章站
2023-12-05 08:10:58
sql server 根据地图经纬度计算距离的公式 复制代码 代码如下: go --创建经纬度距离计算函数 createfunction [dbo].[fngetdista...
sql server 根据地图经纬度计算距离的公式
go
--创建经纬度距离计算函数
createfunction [dbo].[fngetdistance]
--latbegin 开始经度
--lngbegin 开始维度
(@latbegin real, @lngbegin real, @latend real, @lngend real)
returnsfloat
as
begin
--距离(千米)
declare @distance real
declare @earth_radius real
set @earth_radius = 6378.137
declare @radlatbegin real,
@radlatend real,
@radlatdiff real,
@radlngdiff real
set @radlatbegin = @latbegin *pi()/ 180.0
set @radlatend = @latend *pi()/ 180.0
set @radlatdiff = @radlatbegin - @radlatend
set @radlngdiff = @lngbegin *pi()/ 180.0 - @lngend *pi()/ 180.0
set @distance = 2 *asin(
sqrt(
power(sin(@radlatdiff / 2), 2)+cos(@radlatbegin)*cos(@radlatend)
*power(sin(@radlngdiff / 2), 2)
)
)
set @distance = @distance * @earth_radius
--set @distance = round(@distance * 10000) / 10000
return @distance
end
@distance的单位为:千米
复制代码 代码如下:
go
--创建经纬度距离计算函数
createfunction [dbo].[fngetdistance]
--latbegin 开始经度
--lngbegin 开始维度
(@latbegin real, @lngbegin real, @latend real, @lngend real)
returnsfloat
as
begin
--距离(千米)
declare @distance real
declare @earth_radius real
set @earth_radius = 6378.137
declare @radlatbegin real,
@radlatend real,
@radlatdiff real,
@radlngdiff real
set @radlatbegin = @latbegin *pi()/ 180.0
set @radlatend = @latend *pi()/ 180.0
set @radlatdiff = @radlatbegin - @radlatend
set @radlngdiff = @lngbegin *pi()/ 180.0 - @lngend *pi()/ 180.0
set @distance = 2 *asin(
sqrt(
power(sin(@radlatdiff / 2), 2)+cos(@radlatbegin)*cos(@radlatend)
*power(sin(@radlngdiff / 2), 2)
)
)
set @distance = @distance * @earth_radius
--set @distance = round(@distance * 10000) / 10000
return @distance
end
@distance的单位为:千米
下一篇: Sqlserver 表类型和表变量介绍