la funzione qui sotto dà distanza tra due geocoordinates in miglia
create function [dbo].[fnCalcDistanceMiles] (@Lat1 decimal(8,4), @Long1 decimal(8,4), @Lat2 decimal(8,4), @Long2 decimal(8,4))
returns decimal (8,4) as
begin
declare @d decimal(28,10)
-- Convert to radians
set @Lat1 = @Lat1/57.2958
set @Long1 = @Long1/57.2958
set @Lat2 = @Lat2/57.2958
set @Long2 = @Long2/57.2958
-- Calc distance
set @d = (Sin(@Lat1) * Sin(@Lat2)) + (Cos(@Lat1) * Cos(@Lat2) * Cos(@Long2 - @Long1))
-- Convert to miles
if @d <> 0
begin
set @d = 3958.75 * Atan(Sqrt(1 - power(@d, 2))/@d);
end
return @d
end
La funzione seguente fornisce distanza tra due geocoordinates in Km
CREATE FUNCTION dbo.fnCalcDistanceKM(@lat1 FLOAT, @lat2 FLOAT, @lon1 FLOAT, @lon2 FLOAT)
RETURNS FLOAT
AS
BEGIN
RETURN ACOS(SIN(PI()*@lat1/180.0)*SIN(PI()*@lat2/180.0)+COS(PI()*@lat1/180.0)*COS(PI()*@lat2/180.0)*COS(PI()*@lon2/180.0-PI()*@lon1/180.0))*6371
END
utilizzati:
select [dbo].[fnCalcDistanceKM](13.077085,80.262675,13.065701,80.258916)
La funzione seguente fornisce distanza tra due geocoordinates in Km utilizzando Geography tipo di dati che è stato introdotto in SQL Server 2008
DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SET @h = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @g.STDistance(@h);
Riferimento:Ref1, Ref2
Spero che questo aiuta
fonte
2014-03-20 08:06:08
Per il calcolo della distanza in sé si consiglia di guardare a < http://williams.best.vwh.net/avform.htm#Dist> –