在做垃圾桶服务能力检测功能时,需要求两个垃圾桶之间在道路上的实际距离,由于道路使用的是线要素,而考虑到采集数据的精度问题,我假设在地图上道路两边都有可能出现垃圾桶。所以求这个距离的确是个难题。后来想了很多种方法,都一一否定了。
后来在论坛上看见一个帖子,他是使用的vb写的,被我改成c#了,其实也挺简单的,主要使用了IPolyline自带的QueryPointAndDistance方法,它能返回一个点至线的起始点的距离,然后通过这两个距离相减,就得到两点之间的距离了。
ICurve.QueryPointAndDistance Method :Finds the point on the curve closest to inPoint, then copies that point to outPoint; optionally calculates related items。
/// <summary>
/// 得到两个垃圾桶在道路上的距离
/// </summary>
/// <param name="pnt1">垃圾桶1</param>
/// <param name="pnt2">垃圾桶2</param>
/// <param name="roadFeature">所在道路</param>
/// <returns></returns>
public double getDistanceOfTwoPoint(IPoint pnt1, IPoint pnt2, IFeature roadFeature)
{
double distanceOfpnt1 = distanceFromStartPoint(pnt1, roadFeature);
double distanceOfpnt2 = distanceFromStartPoint(pnt2, roadFeature);
return distanceOfpnt2 - distanceOfpnt1;
}
/// <summary>
/// 垃圾桶到道路起点的距离
/// </summary>
/// <param name="pPoint"></param>
/// <param name="pFeature"></param>
/// <returns></returns>
public double distanceFromStartPoint(IPoint pPoint, IFeature pFeature)
{
IPolyline pPolyline = pFeature.Shape as IPolyline;
IPoint outPoint = new PointClass();
double distanceAlongCurve = 0;//该点在曲线上最近的点距曲线起点的距离
double distanceFromCurve = 0;//该点到曲线的直线距离
bool bRightSide = false;
pPolyline.QueryPointAndDistance(esriSegmentExtension.esriNoExtension, pPoint, false, outPoint, ref distanceAlongCurve, ref distanceFromCurve,ref bRightSide);
return distanceAlongCurve;
}