zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    自适应高度的 文本框
    点击小图片遮罩显示大图片
    C++中的声明与定义
    LeetCode_Bit Manipulation
    “纯”面向对象
    指针和引用
    new和delete用法小结
    C++中的变量属性小结
    C++的一些黑暗料理
    Python中的字典和集合
  • 原文地址:https://www.cnblogs.com/Gold-fangjin/p/5948928.html
Copyright © 2011-2022 走看看