------------------------------------------创建一个方法--------------------------------------------------------------------
--@LatBegin 纬度A(33.8703596)--
--@LngBegin 经度A(-117.9242966)--
--@LatEnd 纬度B(34.0392283)--
--@LngEnd 经度B(-117.8367681)--
CREATE FUNCTION [dbo].[fnGetDistance](@LatBegin REAL, @LngBegin REAL, @LatEnd REAL, @LngEnd REAL) RETURNS FLOAT
AS
BEGIN
--距离(千米)
DECLARE @Distance REAL
--距离(英里)--
DECLARE @Edistance REAL
DECLARE @EARTH_RADIUS REAL
DECLARE @MI REAL
SET @EARTH_RADIUS = 6378.137 --地球半径(千米)--
Set @MI=0.6213712 --1千米(km)=0.6213712英里(mi)
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,1) / 10000 --千米--
SET @Edistance= @Distance * @MI --英里--
RETURN @Edistance
END
------------------------------------------删除方法--------------------------------------------------------------------
drop function dbo.fnGetDistance
------------------------------------------调用方法--------------------------------------------------------------------
select dbo.fnGetDistance(33.8703596,-117.9242966,34.0392283,-117.8367681)