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的单位为:千米