Calculate the distance according to the latitude and longitude of the map in sql server

Baked from other places, I tried it myself, I can calculate it, but I don't really understand if it is correct or not.

 

USE [niaoren]
GO
/****** Object: UserDefinedFunction [dbo].[fnGetDistance] Script Date: 2017/7/28 Fri 12:17:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO --Calculate
  the distance between two coordinate points (longitude, latitude) on the earth sql function  
    --Author   : lordbaby --Organization
    : www.aspbc.com   
    ALTER FUNCTION [dbo].[fnGetDistance](@LatBegin REAL, @LngBegin REAL, @LatEnd REAL, @LngEnd REAL) RETURNS FLOAT  
      AS  
    BEGIN  
      -- distance (km)  
      DECLARE @Distance REAL  
      DECLARE @EARTH_RADIUS REAL  
      SET @EARTH_RADIUS = 6378.137 -- earth radius
      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       
      RETURN @Distance  
    END  

 

Original address: http://www.open-open.com/code/view/1436452727411

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326188657&siteId=291194637