2013-04-12 44 views
3

我可以使用下面的SQL來計算固定位置和數據庫中場地之間的距離。.NET等價的SQL Server STDistance

SELECT Location.STDistance(geography::Point(51, -2, 4326)) * 0.00062137119 FROM Venues 

請注意返回的距離以英里爲單位,位置字段是地理類型。

我想知道什麼是相當於.NET的這將返回相同的值。此方法將具有以下簽名:

public static double Distance(location1Latitude, location1Longitude, location2Latitude, location2Longitude) { 
    return ...; 
} 

我知道我可以在.NET中調用數據庫方法,但我不希望這樣做。我希望有一個公式來計算距離。謝謝

+0

的[計算在千米C#兩個地理點距離]可能重複(http://stackoverflow.com/questions/6544286/calculate-distance-of-two-geo-points-in- km-c-sharp) –

回答

4

我相信你可以簡單地將Microsoft.SqlServer.Types.dll作爲參考,然後使用SqlGeometry類型,就像其他任何.NET類型一樣,包括調用STDistance方法。

1

這是very well explained here。 不久:與EF5(更具體,與.net 4.5)微軟包括類型DbGeography。比方說您已經有一堆的緯度/長,你就可以輕鬆地創建使用一個幫手像DbGeography對象:

public static DbGeography CreatePoint(double latitude, double longitude) 
{ 
    var text = string.Format(CultureInfo.InvariantCulture.NumberFormat, 
          "POINT({0} {1})", longitude, latitude); 
    // 4326 is most common coordinate system used by GPS/Maps 
    return DbGeography.PointFromText(text, 4326); 
} 

一旦你有兩個或兩個以上的點(DbGeography)你得到的一切來計算Distance它們之間:

var distance = point1.Distance(point2)