sql server2008根據經緯度計算兩點之間的距離


--通過經緯度計算兩點之間的距離
 create  FUNCTION [dbo].[fnGetDistanceNew] 
 
 --LatBegin 開始經度
 --LngBegin 開始維度
 --29.490295,106.486654,29.615467, 106.581515
 
(@LatBegin1 varchar(128), @LngBegin1 varchar(128),@location varchar(128)) 
     Returns real
       AS
BEGIN
       --轉換location字段,防止字段太長.影響SQL美觀
       declare  @LatBegin REAL
       declare  @LngBegin REAL
       declare  @LatEnd REAL
       declare  @LngEnd REAL
       set @LatBegin=Convert(real,@LatBegin1)
       set @LngBegin=Convert(real,@LngBegin1)
       set @LatEnd=Convert(real,SUBSTRING(@location,0,charindex('&',@location)))
       set @LngEnd=Convert(real,SUBSTRING(@location,charindex('&',@location)+1,LEN(@location)))
      
       --距離(千米)
       DECLARE @Distance      REAL
       DECLARE @EARTH_RADIUS  REAL
       SET @EARTH_RADIUS = 6371.004

       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,5)
       RETURN @Distance
       
END

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM