Sql Server 两个经纬度,计算距离
--计算弧度角
Create function [dbo].[FnRad](@d float)
returns float
as
begin
declare @rad float
set @rad=@d*PI()/180.0
return @rad
end
--通过两个经纬度,计算距离,返回以千米为单位
Create function [dbo].[FnNear](
@lat1 float,--用户当前纬度
@lng1 float,--用户当前经度
@lat2 float,--目标纬度
@lng2 float)--目标经度
returns float
as
begin
declare @EARTH_RADIUS float--地球半径
set @EARTH_RADIUS=6378.137
declare @radLat1 float
set @radLat1=dbo.FnRad(@lat1)
declare @radLat2 float
set @radLat2=dbo.FnRad(@lat2)
declare @a float
set @a=@radLat1-@radLat2
declare @b float
set @b=dbo.FnRad(@lng1)-dbo.FnRad(@lng2)
declare @far float
set @far=2*ASIN(SQRT(POWER(SIN(@a/2),2)+COS(@radLat1)*COS(@radLat2)*POWER(SIN(@b/2),2)))
set @far=@far* @EARTH_RADIUS
set @far=ROUND(@far*10000,0)/10000
return @far
end