Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE FUNCTION CalculateDistance
- (@StartLongitude float,
- @StartLatitude float,
- @EndLongitude float,
- @EndLatitude float)
- RETURNS decimal(7,1)
- AS
- BEGIN
- DECLARE @lat1 float
- DECLARE @lat2 float
- DECLARE @long1 float
- DECLARE @long2 float
- DECLARE @LongDistance float
- DECLARE @LatDistance float
- DECLARE @EarthRadius smallint
- DECLARE @ChordLength float
- DECLARE @CentralAngle float
- SET @lat1 = RADIANS(@StartLatitude)
- SET @lat2 = RADIANS(@EndLatitude)
- SET @long1 = RADIANS(@StartLongitude)
- SET @long2 = RADIANS(@EndLongitude)
- SET @LongDistance = @long2 - @long1
- SET @LatDistance = @lat2 - @lat1
- SET @EarthRadius = 3959
- -- Calucate Chord Length
- SET @ChordLength = SQUARE(SIN(@LatDistance /2)) + COS(@lat1) * COS(@lat2) * SQUARE(SIN(@LongDistance /2))
- SET @CentralAngle = 2 * ATN2(SQUARE(@ChordLength),SQUARE(1-@ChordLength))
- RETURN ROUND(@CentralAngle * @EarthRadius, 1)
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement