我需要一个函数来计算点A和点B之间的距离。
命名空间System.Spatial
似乎正是我所需要的,但我不知道如何使用它。
IPoint
是一个接口,提供Latitude
和Longitude
,均为double
类型。
第一次尝试:
public static double CalculateDistance(IPoint pointA, IPoint pointB)
{
var gp1 = GeographyPoint.Create(pointA.Latitude, pointA.Longitude);
var gp2 = GeographyPoint.Create(pointB.Latitude, pointB.Longitude);
return gp1.Distance(gp2) ?? 0;
}
在point1.Distance(point2)
处出现了NotImplementedException
异常。错误信息为法语,但基本意思是“未注册操作,请使用属性SpatialImplementation.CurrentImplementation.Operations提供操作”。
好的,那我尝试一下这样:
public static double CalculateDistance(IPoint pointA, IPoint pointB)
{
var gp1 = GeographyPoint.Create(pointA.Latitude, pointA.Longitude);
var gp2 = GeographyPoint.Create(pointB.Latitude, pointB.Longitude);
return SpatialImplementation.CurrentImplementation.Operations.Distance(gp1, gp2);
}
现在我在 SpatialImplementation.CurrentImplementation.Operations
上遇到了一个 NullReferenceException
异常。
Msdn 对此并没有过多的说明。
有人可以解释一下如何获取这些实现吗?
IPoint
接口? - Habib