所以,在过去三个小时搞砸了这个之后,我找到了一个解决方案。这是任何关心的人的代码:
public static class DbGeographyExtensions {
/// <summary>
/// Returns a double? value containing the shortest distance from the edge of this polygon to the specified point
/// </summary>
public static double? ToShortestDistanceFromPoint(
this DbGeography polygon,
DbGeography point) {
if ((polygon != null)
&& (point != null)) {
/// Convert the DbGeography to SqlGeography
SqlGeography sqlPolygon = SqlGeography.STPolyFromText(new SqlChars(polygon.AsText()), polygon.CoordinateSystemId);
SqlGeography sqlPoint = SqlGeography.STPointFromText(new SqlChars(point.AsText()), point.CoordinateSystemId);
/// Get the shortest line from the edge of the polygon to the point
SqlGeography shortestPoint = sqlPoint.ShortestLineTo(sqlPolygon);
/// Return the length of the line (distance returns 0 because it excludes the area of the line)
return (double?)shortestPoint.STLength();
}
return null;
}
}