T-SQL 單位(公尺):

Create function dbo.CalculateDistance(@Lat1 float,@Lon1 float,@Lat2 float,@Lon2 float) returns float
as
begin
declare @ret float, @r float, @A float, @B float, @RA float, @RB float, @RL1 float, @RL2 float;
select @r = 6371138, @A = abs(@Lat1 - @Lat2), @B = abs(@Lon1 - @Lon2), @RA = @A * pi() / 180.0, @RB = @B * pi() / 180.0, @RL1 = @Lat1 * pi() / 180.0, @RL2 = @Lat2 * pi() / 180.0, @ret = cast(2 * @r * asin(sqrt(sin(@RA/2) * sin(@RA/2) + cos(@RL1) * cos(@RL2) * sin(@RB/2) * sin(@RB/2))) as decimal(8,0));
return @ret
end

 

C# 單位(公里):

private double GetDistance(double Lat1, double Long1, double Lat2, double Long2)
{
double Lat1r = ConvertDegreeToRadians(Lat1);
double Lat2r = ConvertDegreeToRadians(Lat2);
double Long1r = ConvertDegreeToRadians(Long1);
double Long2r = ConvertDegreeToRadians(Long2);

double R = 6371; // Earth's radius (km)
double d = Math.Acos(Math.Sin(Lat1r) *
Math.Sin(Lat2r) + Math.Cos(Lat1r) *
Math.Cos(Lat2r) *
Math.Cos(Long2r - Long1r)) * R;
return d;
}
private double ConvertDegreeToRadians(double degrees)
{
return (Math.PI / 180) * degrees;
}

AwEi 發表在 痞客邦 PIXNET 留言(0) 人氣()