TSQL 根据经纬度计算两点间的距离;返回米(m)

-- =============================================
-- Author:Forrest
-- Create date: 2013-07-16
-- Description:    根据经纬度计算两点间的距离;返回米(m)
-- =============================================
ALTER FUNCTION [dbo].[Fun_MathRange] 
(
    @in_Lat1 decimal(18,6),
    @in_Lng1 decimal(18,6),
    @in_Lat2 decimal(18,6),
    @in_Lng2 decimal(18,6)
)
RETURNS int
AS
BEGIN
    set @in_Lat1 = @in_Lat1 * PI() / 180.0;
    set @in_Lat2 = @in_Lat2 * PI() / 180.0;
    set @in_Lng1 = @in_Lng1 * PI() / 180.0;
    set @in_Lng2 = @in_Lng2 * PI() / 180.0;
    declare @a decimal(18,6),@b decimal(18,6),@c decimal(18,6)
    set @a = @in_Lat1 - @in_Lat2;
    set @b = @in_Lng1 - @in_Lng2;
    set @c = 2 * Asin(Sqrt(Power(Sin(@a/2),2) + Cos(@in_Lat1)*Cos(@in_Lat2)*Power(Sin(@b/2),2)))
    set @c = @c * 6378.137 --地球半径,单位:千米
    set @c = Round(@c * 10000,3) / 10000
    set @c = @c*1000
    return @c
END

 

posted @ 2013-07-16 10:58  ForrestWu  阅读(278)  评论(0编辑  收藏  举报